2010-08-21 18 views
7

关于如何在通知栏中进行自定义布局,已经有几个线程。问题是我必须错过简单的东西。更新通知区域中的进度栏​​

我有一个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> 

我也有一个创建的通知,其工作方式和进度条显示的一些测试代码。

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); 

最后我尝试更新进度条,这是行不通的。

contentView.setProgressBar(R.id.progressBar, 10, 5, false); 

实际更新通知的秘诀是什么?

+0

看到可能的重复:http://stackoverflow.com/questions/2689729/progress-bar-in-notification-bar – 2011-12-05 10:12:03

回答

10

您应该添加以下两行:

// update the notification object 
notification.contentView.setProgressBar(R.id.progressBar, 10, 5, false); 
// notify the notification manager on the update. 
mManager.notify(APPID, notification); 
+1

这个问题是,它实际上使通知跳转(明智的位置)在通知区域,如果有的话例如其他下载运行也不断刷新他们的进度条...但我不知道你是否可以做些什么... – ubuntudroid 2011-05-31 15:19:10

+2

你可能忘了添加'FLAG_ONGOING_EVENT'到你的通知。 – Xion 2011-08-21 11:45:45

+0

FLAG_ONGOING_EVENT不起作用。通知不断跳跃。在华硕变压器平板电脑上测试。 – 2011-12-05 17:47:18

1

在你的布局文件,你有progressbars最高设置为0。 如果马克塞斯为0,它不能去高于0将其设置为100

4

请记住不要经常通知状态栏。例如,如果你在内部使用该代码,一个AsyncTask的onProgressUpdate,并通知每一个进度,你将会虚拟地阻止状态栏。只有在有变化时才通知。

0

我在更新进度栏时经常遇到问题(我正在使用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(); 

这防止bloking的notiicaton地区,也可以顺利更新的进度条