在一段时间后启动方法

时间:2013-07-27 16:37:24

标签: android alarmmanager

我试图在同一个班级的闹钟管理器中午餐。好吧,在我的服务类中,我有这个方法:

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();
    }

1 个答案:

答案 0 :(得分:2)

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

步骤2:通过调用某些AlarmManager上的getSystemService()来获取Context,例如您的活动

步骤3:致电set()上的AlarmManager,安排PendingIntent在三小时内投放

然而:

  • 如果可能,请考虑使用IntentService,而不是Service,看似AsyncTask

  • 如果您预计即使设备在三小时内处于睡眠模式也要完成此项工作,您将需要使用_WAKEUP式警报以及让设备保持清醒状态工作正在进行,例如WakefulBroadcastReceiverWakefulIntentService