新しいスレッドを使用して、汚い方法でそれを行うことができます。ただし、この方法では、通知自体が終了するまで通知は終了しません。これがコードです。
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();
}