我對Android還是一個新手,我試圖改進通知的進度條,使其更加流暢,並且不會向我的Pebble發送一百萬次更新,並以「正確的方式」執行此操作。這段代碼在我使用它的時候工作「很好」,通知會畫並且進度條按預期完成。Android - 更新通知進度條,正確
當我將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;
}
我認爲問題在於,您每次希望更新通知時都要重新創建構建器。嘗試使用相同的生成器。我認爲這會有所幫助。 –