您可以使用新的線程以骯髒的方式做到這一點。但是,通過這種方式,您的通知不會結束,直到它自行結束。 這是代碼。
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();
}
這個進度條只會在Honeycomb後才起作用.. – tasomaniac 2012-11-05 10:14:36