关于如何在通知栏中进行自定义布局,已经有几个主题.问题是我必须遗漏一些简单的东西.
我有一个custom_notification_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"
>
<TextView android:id="@+id/text"
android:text="Uploading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:max="0"
android:progress="0"
android:layout_marginLeft="10dip"
android:id="@+id/progressBar"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我还有一些创建通知的测试代码,它可以工作并显示进度条.
NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
contentView.setProgressBar(R.id.progressBar, 10, 0, false);
contentView.setTextViewText(R.id.text, text);
notification.contentView = contentView;
Intent notificationIntent = new Intent(context, NotificationHandler.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mManager.notify(APPID, notification);
Run Code Online (Sandbox Code Playgroud)
最后我尝试更新进度条,这不起作用.
contentView.setProgressBar(R.id.progressBar, 10, 5, false);
Run Code Online (Sandbox Code Playgroud)
实际更新通知的秘诀是什么?
小智 11
你应该添加这两行:
// update the notification object
notification.contentView.setProgressBar(R.id.progressBar, 10, 5, false);
// notify the notification manager on the update.
mManager.notify(APPID, notification);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12086 次 |
| 最近记录: |