如何能重复AlarmManager启动AsyncTask的? [英] How can repeating AlarmManager start AsyncTask?

查看:108
本文介绍了如何能重复AlarmManager启动AsyncTask的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我平时写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.

这篇关于如何能重复AlarmManager启动AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆