如何使用onStop()关闭应用程序后运行任务?
问题描述:
我目前正在试验Android我想知道如何让postDelayed在我点击主页按钮后运行?如何使用onStop()关闭应用程序后运行任务?
使用处理程序我能够使postDelayed运行每分钟,以便一个整数增加一个,但我希望整数保持增加,即使我离开应用程序通过按主页按钮。
有解决我的问题吗?
答
您可以使用AlarmManeger
在特定时间内开始Service
。下面是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);
+0
谢谢启动'Service'你为你评论。还没有尝试过,但只要我找到测试我会尝试它。 – ibsof 2015-03-02 20:10:27
使用'AsyncTask'(不是很大)或'Service'(更好) – 2015-02-24 15:14:21
此外,您还可以使用'AlarmManeger'在一个特定的时间 – SergaRUS 2015-02-24 15:15:59