AlarmManager定时开关业务

news/2024/10/17 8:21:28/

AlarmManagerUtils.setAlarmOpen(context, 1, Integer.valueOf(a1), Integer.valueOf(a2), 0, 1, 0, "提醒开启风扇时间到了");

AlarmManagerUtils.setAlarmClose(context, 1, Integer.valueOf(b1), Integer.valueOf(b2), 0, 2, 0, "提醒关闭风扇时间到了");


/**
 * 定时提醒工具类
 */
public class AlarmManagerUtils {
    public static final String ID = "ID";
    public static final String INTERVAL_MILLIS = "INTERVAL_MILLIS";
    public static final String TIPS = "TIPS";

    /**
     * 设置定时提醒,供AlarmOpenService使用
     */
    public static void setAlarmOpenTime(Context context, long timeInMillis, Intent intent) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

//    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0), intent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent sender = PendingIntent.getService(context, intent.getIntExtra(ID, 0), intent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(timeInMillis, sender);
            am.setAlarmClock(alarmClockInfo, sender);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
    }

    /**
     * 设置定时提醒,供AlarmCloseService使用
     */
    public static void setAlarmCloseTime(Context context, long timeInMillis, Intent intent) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        //PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0), intent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent sender = PendingIntent.getService(context, intent.getIntExtra(ID, 0), intent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(timeInMillis, sender);
            am.setAlarmClock(alarmClockInfo, sender);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
    }

    /**
     * @param flag   周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2 表示按周每周提醒的闹钟(一周的周期性时间间隔)
     * @param hour   时
     * @param minute 分
     * @param second 秒
     * @param id     闹钟的id
     * @param week   week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
     * @param tips   闹钟提示信息
     *               //     * @param soundOrVibrator 0表示只有震动提醒 1表示只有铃声提醒 2表示声音和震动都执行
     */
    @SuppressLint("ShortAlarm")
    public static void setAlarmOpen(Context context, int flag, int hour, int minute, int second, int id, int week, String tips) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        long intervalMillis = 0;
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hour, minute, second);

        if (flag == 0)
            intervalMillis = 0;
        else if (flag == 1)
            intervalMillis = AlarmManager.INTERVAL_DAY;
        else if (flag == 2)
            intervalMillis = AlarmManager.INTERVAL_DAY * 7;

        Intent intent = new Intent(AlarmOpenService.ACTION);
        intent.putExtra(ID, id);
        intent.putExtra(TIPS, tips);
        intent.putExtra(INTERVAL_MILLIS, intervalMillis);

        PendingIntent sender = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        long time = calMethod(week, calendar.getTimeInMillis());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, sender);
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(time, sender);
            am.setAlarmClock(alarmClockInfo, sender);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            am.setExact(AlarmManager.RTC_WAKEUP, time, sender);
        else
            am.setRepeating(AlarmManager.RTC_WAKEUP, time, intervalMillis, sender);// 可能存在不精确的问题
    }

    /**
     * @param flag   周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2 表示按周每周提醒的闹钟(一周的周期性时间间隔)
     * @param hour   时
     * @param minute 分
     * @param second 秒
     * @param id     闹钟的id
     * @param week   week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
     * @param tips   闹钟提示信息
     *               //     * @param soundOrVibrator 0表示只有震动提醒 1表示只有铃声提醒 2表示声音和震动都执行
     */
    @SuppressLint("ShortAlarm")
    public static void setAlarmClose(Context context, int flag, int hour, int minute, int second, int id, int week, String tips) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        long intervalMillis = 0;
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hour, minute, second);

        if (flag == 0)
            intervalMillis = 0;
        else if (flag == 1)
            intervalMillis = AlarmManager.INTERVAL_DAY;
        else if (flag == 2)
            intervalMillis = AlarmManager.INTERVAL_DAY * 7;

        Intent intent = new Intent(AlarmCloseService.ACTION);
        intent.putExtra(ID, id);
        intent.putExtra(TIPS, tips);
        intent.putExtra(INTERVAL_MILLIS, intervalMillis);

        PendingIntent sender = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        long time = calMethod(week, calendar.getTimeInMillis());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, sender);
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(time, sender);
            am.setAlarmClock(alarmClockInfo, sender);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            am.setExact(AlarmManager.RTC_WAKEUP, time, sender);
        else
            am.setRepeating(AlarmManager.RTC_WAKEUP, time, intervalMillis, sender);// 可能存在不精确的问题
    }

    public static void cancelAlarm(Context context, String action, int id) {
        Intent intent = new Intent(action);
//    PendingIntent pi = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent pi = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pi);
    }

    /**
     * triggerAtMillis 计算方法
     *
     * @param weekflag 传入的是周几
     * @param dateTime 传入的是时间戳(设置当天的年月日+从选择框拿来的时分秒)
     * @return 返回起始闹钟时间的时间戳
     */
    private static long calMethod(int weekflag, long dateTime) {
        long time = 0;
        //weekflag == 0表示是按天为周期性的时间间隔或者是一次行的,weekfalg非0时表示每周几的闹钟并以周为时间间隔
        if (weekflag != 0) {
            Calendar c = Calendar.getInstance();
            int week = c.get(Calendar.DAY_OF_WEEK);
            if (1 == week) {
                week = 7;
            } else if (2 == week) {
                week = 1;
            } else if (3 == week) {
                week = 2;
            } else if (4 == week) {
                week = 3;
            } else if (5 == week) {
                week = 4;
            } else if (6 == week) {
                week = 5;
            } else if (7 == week) {
                week = 6;
            }

            if (weekflag == week) {
                if (dateTime > System.currentTimeMillis()) {
                    time = dateTime;
                } else {
                    time = dateTime + 7 * 24 * 3600 * 1000;
                }
            } else if (weekflag > week) {
                time = dateTime + (weekflag - week) * 24 * 3600 * 1000;
            } else {
                time = dateTime + (weekflag - week + 7) * 24 * 3600 * 1000;
            }
        } else {
            if (dateTime > System.currentTimeMillis()) {
                time = dateTime;
            } else {
                time = dateTime + 24 * 3600 * 1000;
            }
        }
        return time;
    }
}

public class AlarmOpenService extends Service {public static final String ACTION = "org.tcshare.app.alarmopen";private static final String TAG = "aaaaa";private Boolean onOff = true;private Thread openThread;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Context context = getApplicationContext();openThread = new Thread() {@Overridepublic void run() {long intervalMillis = intent.getLongExtra(AlarmManagerUtils.INTERVAL_MILLIS, 0);if (intervalMillis != 0) {AlarmManagerUtils.setAlarmOpenTime(context, System.currentTimeMillis() + intervalMillis, intent);Log.e(TAG, "开启业务 ");}}}};openThread.start();return super.onStartCommand(intent, flags, startId);}
}


 


http://www.ppmy.cn/news/429078.html

相关文章

系统定时任务linux,linux系统下定时任务(示例代码)

linux系统定时任务 1、Linux系统定时任务at (1)作用:定时任务,指定一个时间执行一个任务,只能执行一次 (2)用法:at [option] TIME 选项:-V 显示版本信息: -l:列出指定队列中等待运行的作业;相当于atq -d:删…

编写一个闹钟和定时关机工具(MFC VS2010)

这个小工具在自己生活当中能用到,运行软件以后,会显示当前的系统时间,然后你可以设定时间,再选择是定时响铃还是关机。截图如下: 前言:本程序采用visual studio 2010 ,对话框类型的应用程序&…

linux cpu跑分软件,Geekbench 5 5.2.3 硬件性能跑分工具 特别版

CPU基准测试 Geekbench 5测量处理器的单核和多核电源,从查看电子邮件到拍照到播放音乐,或者一次性完成所有操作。Geekbench 5的CPU基准测试可以测量新应用领域的性能,包括增强现实和机器学习,因此您将了解系统与前沿的接近程度。 …

软路由cpu性能跑分

软路由cpu性能跑分 cpu核心功耗单核多核N50304/46W14052909N50004/46W11522608N41204/46W11072477N41004/46W9952238N42004/46W8362027N34504/46W7341842J19004/410W5431797 引用: youtube老白

跑分cpu_一款神秘的联发科CPU:跑分达到了622409分

超过10万人正在关注 赶快来关注吧,这里有你想找的热点资讯,这里有你想要的各种资料,还有海量的资源,还在等什么。快来关注,大佬带你开车。 11月30日,安兔兔官方微博曝光了一款神秘的联发科CPU。这款CPU采用…

java cpu 分析工具_java性能分析 - CPU飙高分析工具

背景 有处理过生产问题的同学基本都能遇到系统忽然缓慢,CPU突然飙升,甚至整个应用请求不可用。当出现这种情况下,在不影响数据准确性的前提下,我们应该尽快导出jstack和内存信息,然后重启系统,尽快回复系统…

跑分cpu_【新机】天玑800+跑分性能出炉:CPU干翻骁龙765G,比肩麒麟985 | 骁龙768G性能曝光...

近日一款型号为M2004J7BC的小米新机出现在了GeekBench跑分库中,应该就是即将亮相发布的redmi Note 9系列。新机搭载了天玑820,型号为MT6875(天玑800为MT6873)。CPU由4颗2.6GHz的A76大核4颗2.0GHz的A55小核构成,GPU为Mali-G57。 Geekbench5 天…

跑分cpu_跑分完爆骁龙 865?明年这些中端处理器真的要起飞

2013 年,骁龙 800 处理器手机的安兔兔跑分,是 3.3 万分。 2020 年,骁龙 865 处理器手机跑分是 —— 67 万分。(这个分数要记住,重点) 七年时间,同一系列在同一平台跑出了将近 20 倍的分数差距。 这,也就是手…