简体   繁体   中英

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?

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.

Is there solution to my problem?

You can use AlarmManeger to start your Service in a specific time. Here is an example with 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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