android 使用AlarmManager定时启动service

    private static AlarmManager am;
    private static PendingIntent pendingIntent;

    /**
     * 使用 AlarmManager 来 定时启动服务
     */
    public static void startPendingIntent(Context context) {

        am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(context, MyService.class);//启动示例Service

        pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        long interval = DateUtils.MINUTE_IN_MILLIS * 30;// 30分钟一次

        long firstWake = System.currentTimeMillis() + interval;

        am.setRepeating(AlarmManager.RTC, firstWake, interval, pendingIntent);

    }

    public static void stopPendingIntent() {

        if (pendingIntent != null) {

            pendingIntent.cancel();

        }
    };

 

posted @ 2014-04-25 10:25  mhx_pyw  阅读(1654)  评论(0编辑  收藏  举报