简体   繁体   English

如何在使用onStop()关闭应用程序后运行任务?

[英]How to run tasks after closing app with onStop()?

I am currently experimenting with Android I was wondering how i can make postDelayed run after I hit the home button? 我正在尝试使用Android我想知道如何在点击主页按钮后进行postDelayed运行?

Using handler I am able to make the postDelayed run every minute so that a integer is increased by one, but I want the integer to keep increasing even after I leave the app by pressing the home button. 使用处理程序我能够每分钟使postDelayed运行,以便整数增加1,但我希望即使在我按下主页按钮离开应用程序后,整数也会继续增加。

Is there solution to my problem? 我的问题有解决方案吗?

You can use AlarmManeger to start your Service in a specific time. 您可以使用AlarmManeger在特定时间启动Service Here is an example with NotificationService : 以下是NotificationService的示例:

Log.i(TAG, "Setting intent");
Intent intent = new Intent(context, NotificationService.class);

Log.i(TAG, "Setting pendingIntent");
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Log.i(TAG, "Setting alarm");
alarmManager.set(AlarmManager.RTC_WAKEUP, time.getMillis(), pendingIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM