美文网首页
定时器AlarmManager使用

定时器AlarmManager使用

作者: 金卡戴珊ugly | 来源:发表于2019-10-10 15:30 被阅读0次

系统定时器能否做到连续一个月甚至永远定时工作。公司代码中使用AlarmManager未验证。所以自己写一个demo记录下。在我们手机中如果app退出了。我们这个alarm肯定失效了。
MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        AlarmSettings.getInstance(this).userAlarms("09:58#10:10");
    }

AlarmSettings

public class AlarmSettings {
    public final static String ACTION_DTGG_ALARM_WORKING = "com.eostek.dtgg.alarm.working";
    public final static String ACTION_DTGG_ALARM_REST = "com.eostek.dtgg.alarm.rest";
    private AlarmManager am = null;
    private Context mContext = null;
    private int[] working_time = null;
    private int[] rest_time = null;
    private final int REQUEST_CODE_WORKING = 1001;
    private final int REQUEST_CODE_REST = 2001;
    private int requestCode;
    private static AlarmSettings instance;

    public static AlarmSettings getInstance(Context context) {
        if (instance == null) {
            synchronized (AlarmSettings.class) {
                if (instance == null) {
                    instance = new AlarmSettings(context);
                }
            }
        }
        return instance;
    }

    public AlarmSettings(Context cxt) {
        mContext = cxt;
        am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    }

    public void userAlarms(String time) {
        Log.e("manley", "userAlarms time = " + time);
        cancelAlarm(false);
        initTime(time);
        startAlarm();
    }

    private void startAlarm() {
        setAlarm(ACTION_DTGG_ALARM_WORKING);
        setAlarm(ACTION_DTGG_ALARM_REST);
    }

    public void setAlarm(String action) {
        int[] times = null;
        if (action.equals(ACTION_DTGG_ALARM_WORKING)) {
            times = working_time;
            requestCode = REQUEST_CODE_WORKING;
        } else if (action.equals(ACTION_DTGG_ALARM_REST)) {
            times = rest_time;
            requestCode = REQUEST_CODE_REST;
        }

        Intent intent = new Intent(action);
        intent.setAction(action);
        PendingIntent pi = PendingIntent.getBroadcast(mContext, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long systemTime = System.currentTimeMillis();

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(systemTime);
        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        if(times!=null){
            calendar.set(Calendar.HOUR_OF_DAY, times[0]);
            calendar.set(Calendar.MINUTE, times[1]);
        }else{
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
        }
        calendar.set(Calendar.SECOND, 0);//times[2]
        calendar.set(Calendar.MILLISECOND, 0);
        long selectTime = calendar.getTimeInMillis();

        Log.d("xiangy", "systemTime = " + systemTime);
        Log.d("xiangy", "selectTime = " + selectTime);

        if (systemTime > selectTime) {
            Log.e("AlarmSettings", "---------------------明天此时执行-------------------"+action);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            selectTime = calendar.getTimeInMillis();
        }

        am.setExact(AlarmManager.RTC_WAKEUP, selectTime, pi);
    }

    public void cancelAlarm(boolean noAlarms) {
        if (am != null) {
            Log.e("zzp", "取消定时闹钟");
            Intent intent_working = new Intent(ACTION_DTGG_ALARM_WORKING);
            am.cancel(PendingIntent.getBroadcast(mContext, REQUEST_CODE_WORKING, intent_working,
                    PendingIntent.FLAG_UPDATE_CURRENT));//
            Intent intent_rest = new Intent(ACTION_DTGG_ALARM_REST);
            am.cancel(PendingIntent.getBroadcast(mContext, REQUEST_CODE_REST, intent_rest,
                    PendingIntent.FLAG_UPDATE_CURRENT));//
        }
        if (noAlarms) {
            //这里发广播导致取消无效
            Log.e("zzp", "取消定时闹钟--------ALL_DAY");
        }
    }

    private void initTime(String time) {
        // "12:00:00#12:00:00"
        Log.e("manley", "------------initTime: "+time);
        String[] times = time.split("#");
        String work = times[0];
        String rest = times[1];
        Log.d("xiangy", "work = " + work);
        Log.d("xiangy", "rest = " + rest);
        this.working_time = splitTime1(work);
        this.rest_time = splitTime1(rest);
    }
    
    private static int[] splitTime1(String time) {
        int[] times = new int[2];
        String[] temp = time.split(":");
        String str = "";
        for (int i = 0; i < 2; i++) {
            times[i] = Integer.parseInt(temp[i]);
            str += times[i] + ((i == 1) ? "" : ":");
        }
        Log.d("xiangy", "str = " + str);
        return times;
    }
}

和投影仪不同是,手机上无法保持app一直运行。需要避免被系统回收才行。
写了三种保活方式,试验下哪个能支撑到24后的明天。

相关文章

网友评论

      本文标题:定时器AlarmManager使用

      本文链接:https://www.haomeiwen.com/subject/ymyiuctx.html