问题 Android - 正确更新通知进度条


我仍然是Android的新手,我正在努力改进我的通知进度条以使其更顺畅,而不是为我的Pebble点燃一百万次更新并以“正确的方式”进行。此代码工作“正常”,就像我使用它时,通知绘制和进度条按预期完成。

当我将Pebble手表设置为接受我的应用程序通知时,这对我来说成了一个问题。这导致每个上传的图像振动约50次,具体取决于上传速度的速度。

作为一个初学者,我认为我这样做是错的,并且有更好的方法来做我想做的事情。

我的通知的进度条使用以下代码更新:

private int upload_progress;
private Long time_previous_progress = Calendar.getInstance().getTimeInMillis();

protected void onProgressUpdate(Integer... progress) {
    Long time_now = Calendar.getInstance().getTimeInMillis();
    if(
       ((time_now - time_previous_progress) < 55) // 55ms minimum delay
       || (progress[0] < 0 && progress[0] > 100)  // progress >0 && <100
       || progress[0].equals(upload_progress)     // progress changed
       || ! App.getStatus()                       // watcher is running
       )
    {
        return;
    }
    time_previous_progress = time_now;
    upload_progress = progress[0];
    int upload_counter = getUploadCounter();
    int upload_total = db.getReadyImagesCount();
    NotificationHandler.notify(context, upload_progress, upload_counter, (upload_total + upload_counter));
}

然后使用以下代码生成通知:

public static int notify(Context context, Integer progress, Integer upload_count, Integer upload_total)
{
    Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    String notif_title = context.getResources().getString(R.string.instant_upload_title);

    String notif_progress = context.getResources().getString(R.string.instant_upload_progress);
    String notif_ticker = String.format(notif_progress, upload_count, upload_total);
    String notif_msg = String.format(notif_progress, upload_count, upload_total);

    Intent intent_swipe = new Intent(context, NotifyReceiver.class);
    intent_swipe.setAction("notification_cancelled");
    PendingIntent pendingIntent_swipe = PendingIntent.getBroadcast(context, 0, intent_swipe, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent intent_click = new Intent(context, Settings.class);
    intent_click.putExtra("notification_clicked", true);
    PendingIntent pendingIntent_click = PendingIntent.getActivity(context, 0, intent_click, PendingIntent.FLAG_CANCEL_CURRENT);

    int pro_max = 100;
    int pro_cur = 0;
    if(progress < 100)
    {
        pro_cur = progress;
    }else{
        pro_cur = pro_max = 0;
    }

    //new NotificationCompat.Builder(context)
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) //PixelRelayApplication.getNotificationBuilder()
        .setTicker(notif_ticker)
        .setContentTitle(notif_title)
        .setContentText(notif_msg)
        .setNumber(upload_count)
        .setSmallIcon(R.drawable.ic_launcher)
        .setLargeIcon(bm)
        .setContentIntent(pendingIntent_click)
        .setDeleteIntent(pendingIntent_swipe)
        .setAutoCancel(true)
        .setOngoing(true)
        .setWhen(Calendar.getInstance().getTimeInMillis())
        .setProgress(pro_max, pro_cur, false);

    Notification notification = builder.build();

    // Put the auto cancel notification flag
    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFY_ME_ID, notification);
    return NOTIFY_ME_ID;
}

5735
2017-12-20 19:54


起源

我认为问题是您每次要更新通知时都重新创建构建器。尝试使用相同的构建器。我认为这会有所帮助。 - android developer


答案:


您可以使用正常工作的开源代码,以便通过Github访问它的想法: Progress Watch - 鹅卵石的进度条表盘。如果您决定重用代码,我的建议是:

  • 给予适当的信用
  • 如果您仍然发现某些内容可以改进,请通知作者
  • 如果添加一些新功能,请创建一个分叉并提交补丁

10
2017-12-25 22:05





我通过在第一次通知后设置正在进行的标记来修复此问题。

因此,重用构建器,并在发送第一个通知后,更新构建器,以便创建持续通知:

builder.setOngoing(真);

当然这也意味着更新的通知将会持续进行, http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing(boolean)


2
2017-12-16 13:22





更新进度的行为是正确的。它应该在有任何变化时立即更新。

但是您应该只更新UI部分(如进度条更新)。

如果您使用Vibrate通知而不使用振动。


0
2017-12-25 19:30



谢谢,我如何才更新进度条? - Jayrox