我正在运行一个处理程序以在后台显示一些通知。在测试时,当我将延迟限制设置为 5 秒时,它可以完美运行。但每当我将它设置为 60 秒或更长时间时,它就不起作用。这是为什么?
int delay = 1000 * 60;
HandlerThread hThread = new HandlerThread("HandlerThread");
hThread.start();
Handler handler = new Handler(hThread.getLooper());
Runnable task = new Runnable() {
@Override
public void run() {
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent launchIntent = new Intent(getApplicationContext(), AndroidLauncher.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidLauncher.this);
//Set notification information
builder.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hi!")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentTitle(mytitle)
.setContentText(mytext)
.setContentIntent(contentIntent);
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(builder);
style.setSummaryText("4 Task");
style.addLine("Two");
style.addLine("Three");
style.addLine("Four");
style.addLine("Five");
Notification note = style.build();
manager.notify(50, note);
}
};
handler.postDelayed(task, delay);
最佳答案
导入 导入android.os.Handler;
并使用以下代码:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//do your work here after 60 second
}
},60000);
关于Handler
、HandlerThread
、Looper
有很好的教程here .
关于android - 处理程序延迟 60 秒不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309904/