2012-07-17 77 views
1

有誰知道我可以如何使用Notification.builder,然後更新進度條?目前它似乎受到限制。我明白我可以訪問notification.contentView,但從這裏更新progressView需要視圖ID,它是android內部類(我沒有範圍訪問)的一部分。當然,如果通知構建器允許您顯示進度條,那麼應該有方法來更新它?修改Notification.Builder進度條沒有自定義佈局

我應該補充一點,我希望能夠在不重複調用'getNotification'的情況下做到這一點,因爲這看起來效率極低。

回答

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(); 
} 
+1

這個進度條只會在Honeycomb後才起作用.. – tasomaniac 2012-11-05 10:14:36