调度两个任务随后的android [英] schedule two tasks subsequently in android

查看:110
本文介绍了调度两个任务随后的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行两个任务。
首先应每隔10min重复一次
其次应该重复的每一分钟。

打开网站的第一个任务
在开第二个任务另一个网站。
感谢名单提前

i want to perform 2 tasks. First should repeat once in every 10min Second should repeat every minute. Example Opening a website in first task Opening another website in second task. Thanx in advance

推荐答案

有关调度部分,你可以使用的 AlarmManager

For the scheduling part you can use the AlarmManager

例如:

public class TaskScheduler {
    public static void startScheduling(Context context) {

            Intent intent = new Intent(context, MyReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 600, pendingIntent);

    }
}

那么你的接收器类中你就可以开始一个 IntentService

public class MyReceiver extends BroadcastReceiver {    
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intentService = new Intent(context, MyService.class);
        context.startService(intentService);
    }
}

为MyService 大体类似:

class MyService extends IntentService {
    public MyService() { 
        super(MyService.class.getSimpleName());
    }

    @Override
    public void onHandleIntent(Intent intent) {
        // your code goes here
    }
}

最后,不要忘记注册 MyReceiver 在manifest文件中:

And finally, don't forget to register MyReceiver in the manifest file:

<receiver 
   android:name="Your.Package.MyReceiver">
</receiver>

,以及你的服务:

As well as your service:

<service 
   android:name="...">
</service>

这篇关于调度两个任务随后的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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