android 6.0 以上在doze模式精确定时

    public static void start12hAlarm() {
        int seconds = TIMERLENGTH;
        ECMLog.i_ecms(CLASS_TAG, " start12hAlarm is called ." );
        AlarmManager manager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(mContext, cls);
        intent.setAction(mAction);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long triggerAtTime = SystemClock.elapsedRealtime();
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime + seconds* 1000, pendingIntent);
        if (Build.VERSION.SDK_INT >= 23) {
            manager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime + seconds* 1000, pendingIntent);
        } else if (Build.VERSION.SDK_INT >= 19) {
            manager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime + seconds* 1000, pendingIntent);
        } else {
            manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime + seconds* 1000, pendingIntent);
        }
    }
   public static void stop12hAlarm() {
        ECMLog.i_ecms(CLASS_TAG, " stop12hAlarm is called ." );
        AlarmManager manager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(mContext, cls);
        intent.setAction(mAction);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        manager.cancel(pendingIntent);
    }

定时器到了发送广播:在广播中进行业务操作

/*
 * 文件名:MainBroadcastReceiver.java
 * 版权:版权所有 (C) 中国电科30所三部
 * 描述:
 * 修改人: chen.qiang
 * 修改时间:2015/10/16
 * 修改内容:新增
 */
package com.cetcs.ecmapplication.ecms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.cetcs.ecmapplication.mqtt.Model;
import com.cetcs.ecmcommon.ECMLog;
import com.cetcs.jniencardmanager.JniEnCardManager;


/**
 * <功能简述>
 * <功能详述>
 *
 * @author liucheng
 * @version 0.1, 2016/9/5
 */
public class AlarmEventReceiver extends BroadcastReceiver {
    private static String CLASS_TAG = "AlarmEventReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        ProcessBroadcast server = new ProcessBroadcast(context);
        String action = intent.getAction();
        ECMLog.i_ecm(CLASS_TAG ," receive a broadcast@@@@: " + action);
        if(server != null) {
            server.startSecApps();
        }
        if(action == null){
            return;
        }
        Utils.startEcmsByBroadcast(context);
        if("android.intent.action.12h_alarm".equals(intent.getAction())){
            ECMLog.i_ecm(CLASS_TAG ," onReceive 12h_alarm");
            if(server != null){
                server.BootReportAlarmBroadcast();
            }
        }
        if(BootReportAgent.mSCSAction.equals(intent.getAction())){
            ECMLog.i_ecm(CLASS_TAG ,"onReceive android.intent.action.scs_socket alarm");
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        int rst = JniEnCardManager.getInstance().isScsSoketException();
                        ECMLog.i_ecm(CLASS_TAG ,"onReceive isScsSoketException() is :"+rst);
                    } catch (Exception e) {
                        e.printStackTrace();
                        ECMLog.i_ecm(CLASS_TAG ,"onReceive isScsSoketException() exception :"+ e.getMessage());
                    }
                }
            });

        }
    }
}
原文地址:https://www.cnblogs.com/kebibuluan/p/7873212.html