我平时写code,开始与 AlarmManager
的服务。
I usually write this code to start a service with AlarmManager
.
intent = new Intent(getActivity(), someservice.class);
pendingNotificationIntent = PendingIntent.getService(getActivity(), 0, intent, 0);
alarm = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
int interval = 30 * 1000;
long now = Calendar.getInstance().getTimeInMillis();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, now, interval, pendingNotificationIntent);
我的的AsyncTask
是一个私有类,我注册 AlarmManager
对象。
My AsyncTask
is a private class where I register AlarmManager
object.
使用我如何可以调用的的AsyncTask
,而不是一种服务 AlarmManager
对象?
How can I call the AsyncTask
instead of a service using AlarmManager
object?
PS。如果有更好的方式来运行的AsyncTask
每隔X秒/分钟,请申请吧!
PS. If there is a better way to run AsyncTask
every X seconds/minutes, please propose it!
这是你如何能做到这一点:
This is how you can do it:
1)定义一个静态的意图(intent1),并用它设定时间时,传递给AlarmManager。所以,现在当过一次,就会失效; AlarmManager将通过发送intent1通知。
1.) Define a static intent (intent1) and use it to pass to AlarmManager when setting time. So now when ever time, will lapse; AlarmManager will notify by sending intent1.
2。)在intent1中的BroadcastReceiver中的onReceive,启动AsyncTask的。在AsyncTask的年底结束,设定下一次AlarmManager。
2.) On onReceive of BroadcastReceiver of intent1, start a AsyncTask. At end end of AsyncTask, set the next time for AlarmManager.