1

有人知道我如何使用 Notification.builder 然后更新进度条吗?目前它似乎受到限制。我知道我可以访问 notification.contentView 但是从这里更新 progressView 需要视图 ID,它是 android 内部类的一部分(我没有访问范围)。当然,如果通知生成器允许您显示进度条,应该有办法更新它吗?

我应该补充一点,我希望在不重复调用“getNotification”的情况下做到这一点,因为这似乎非常低效。

4

1 回答 1

1

您可以使用新线程以肮脏的方式执行此操作。但是,通过这种方式,您的通知不会结束,直到它自己结束。这是代码。

public void showJobFinishNotification() {
    final String ns = Context.NOTIFICATION_SERVICE;
    final NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
    final Intent notificationIntent = new Intent(this, WillingActivity.class);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    final int JOBFINISH_ID = 1;

    new Thread(new Runnable() {
        public void run() {
            for(int progress = 0; progress <= 29; progress++) {
                Notification jobFinishNotification = new NotificationCompat.Builder(getApplication())
                    .setSmallIcon(R.drawable.ic_launcher) //must have small icon, but the large icon is not a must
                    .setContentTitle("Job is finished")
                    .setContentText("Press here to finish your job.")
                    .setProgress(29, progress, false) 
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setContentIntent(contentIntent)
                    .setAutoCancel(false)
                    .build();
                notificationManager.notify(JOBFINISH_ID, jobFinishNotification);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            notificationManager.cancel(JOBFINISH_ID); //if you want the user to handle the notification.
            //comment the line above. and use setAutoCancel(true) in the above code.
        }
    }).start();
}
于 2012-09-05T03:03:03.637 回答