【问题标题】:Updating the Progress Bar in the Notification Area更新通知区域的进度条
【发布时间】:2010-08-21 13:17:18
【问题描述】:

关于如何在通知栏中进行自定义布局已经有几个主题。问题是我一定遗漏了一些简单的东西。

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

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

【问题讨论】:

标签: android


【解决方案1】:

你应该添加这两行:

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

【讨论】:

  • 这个问题是,它实际上使通知在通知区域中跳转(位置明智),如果有例如正在运行的其他下载也不断刷新他们的进度条...但是我不知道您是否可以对此做些什么...
  • 您可能忘记在通知中添加FLAG_ONGOING_EVENT
  • FLAG_ONGOING_EVENT 没有帮助。通知一直跳。在华硕变压器平板电脑上测试。
  • 对于跳跃问题,使用标志Notification.FLAG_ONLY_ALERT_ONCE
  • 对我不起作用 - 尝试过 FLAG_ONGOING_EVENT | FLAG_ONLY_ALERT_ONCE 也是。我发现华硕Transformer TF101在插上底座时显示“Docking connected”,插上PC时显示“ASUS Sync”,只要打开这些通知中的任何一个都会引起跳动。当您下载应用程序时,Google Play 商店也会出现这种情况。
【解决方案2】:

记住不要太频繁地通知状态栏。如果您在内部使用该代码,例如,AsyncTask 的 onProgressUpdate 并通知每个进度,您实际上会阻塞状态栏。仅在有更改时通知。

【讨论】:

    【解决方案3】:

    在您的布局文件中,您将进度条最大值设置为 0。 如果最大值为 0,则不能高于 0。将其设置为 100

    【讨论】:

      【解决方案4】:

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

      这可以防止阻塞通知区域,还可以平滑更新进度条

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        相关资源
        最近更新 更多