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

        }
    }
}
复制代码

 

posted on   luzhouxiaoshuai  阅读(434)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示