Android 更新通知区域中的进度条
关于如何在通知栏中进行自定义布局,已有多个线程。问题是我肯定错过了一些简单的东西 我有一个自定义的\u通知\u layout.xmlAndroid 更新通知区域中的进度条,android,Android,关于如何在通知栏中进行自定义布局,已有多个线程。问题是我肯定错过了一些简单的东西 我有一个自定义的\u通知\u layout.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"
<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>
最后,我尝试更新进度条,但它不起作用
contentView.setProgressBar(R.id.progressBar, 10, 5, false);
实际更新通知的秘诀是什么?您应该添加以下两行:
// update the notification object
notification.contentView.setProgressBar(R.id.progressBar, 10, 5, false);
// notify the notification manager on the update.
mManager.notify(APPID, notification);
在布局文件中,progressbars最大值设置为0。
如果最大值为0,则不能高于0。将其设置为100记住不要经常通知状态栏。例如,如果在AsyncTask的onProgressUpdate中使用该代码并通知每个进度,则实际上会阻塞状态栏。仅在有更改时通知。我在过于频繁地更新进度条时遇到问题(我正在使用
NotificationCompat.Builder
执行此任务),这导致通知区域阻塞。我通过跳过在最短间隔时间内发生的更新来解决此问题,如下所示:
private static final long MIN_UPDATE_INTERVAL = 10000000;
private long lastUpdateTime = System.nanoTime();
在我的更新回调中:
// Don't update too often
if ((System.nanoTime() - lastUpdateTime)< MIN_UPDATE_INTERVAL) return;
builder.setProgress(max, currentValue, false);
notificationManager.notify(notificationID, builder.build());
lastUpdateTime = System.nanoTime();
//不要太频繁地更新
if((System.nanoTime()-lastUpdateTime)
这可以防止阻塞Notiicton区域,还可以顺利更新进度条问题在于,如果有其他下载也在不断刷新进度条,它实际上会使通知在通知区域中跳跃(按位置)。。。但是,我不知道您是否可以对此做些什么……您可能忘记了在通知中添加
FLAG\u continuous\u EVENT
。FLAG\u continuous\u EVENT没有帮助。通知一直在跳。在华硕transformer平板电脑上测试。对于跳跃问题,请使用flag通知。flag\u ONLY\u ALERT\u ONCE
对我不起作用-尝试了flag\u持续事件| flag\u ONLY\u ALERT\u ONCE。我发现Asus Transformer TF101在插入到dock时显示“Docking connected”,在插入到PC时显示“Asus Sync”,并且只要这些通知中的任何一个处于打开状态,就会导致跳线。当你下载应用程序时,谷歌Play Shop也会出现这种跳跃。请参阅可能的重复:
// Don't update too often
if ((System.nanoTime() - lastUpdateTime)< MIN_UPDATE_INTERVAL) return;
builder.setProgress(max, currentValue, false);
notificationManager.notify(notificationID, builder.build());
lastUpdateTime = System.nanoTime();