0

我正在尝试从同一个班级的警报管理器中获得一种方法。好吧,在我的服务类中,我有这个方法:

new loadSomeStuff().execute();

如何使用警报管理器在 3 小时后启动它?

public class MyIntentService extends Service {
    SharedPreferences prefs;
    public static String prefName = "SecretFile";
    public static String version = "56";
    public final static int uniqueID = 1394885;

    public void onCreate() {
        super.onCreate();
        //i want after the alarm manager stop to launch this method.
        new loadSomeStuff().execute();
    }
4

1 回答 1

2

步骤#1:创建一个PendingIntent将启动您的服务

AlarmManager步骤#2:通过调用getSystemService()一些来获取一个Context,例如你的活动

第 3 步:致电安排您set()在三个小时内运行AlarmManagerPendingIntent

然而:

  • 如果可能,请考虑使用 a IntentService,而不是Service看起来像AsyncTask

  • 如果您预计即使设备在三个小时后处于睡眠模式也能完成这项工作,您将需要使用_WAKEUP-style 警报和其他东西来让设备在工作进行时保持清醒,例如WakefulBroadcastReceiverWakefulIntentService

于 2013-07-27T16:46:03.543 回答