阅读更多

AlarmManager 可以設置固定時間,固定周期性的操作

120秒後每60秒調用一次UpdateService:

 private void setAlarmTime(){        Slog.i("TEST", "Start self-define alarm");        currentSystemTime = System.currentTimeMillis();        try {            nextCheckAlarmTime = Long.parseLong(Settings.System.getString(mSystemContext.getContentResolver(), Settings.System.KEY_NEXT_CHECK_TIME));        }catch (Exception e){            e.printStackTrace();        }        manager = (AlarmManager) mSystemContext.getSystemService(ALARM_SERVICE);        Intent intent = new Intent();        intent.setComponent(new ComponentName("com.android.settings","com.android.settings.system.UpdateService"));        pi = PendingIntent.getService(mSystemContext,0,intent,0);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {            if (nextCheckAlarmTime - currentSystemTime > 0) {                manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 120 * 1000 + SystemClock.elapsedRealtime(), 600 * 1000, pi);            } else {                Intent intentSerive = new Intent();                intentSerive.setComponent(new ComponentName("com.android.settings", "com.android.settings.system.UpdateService"));                mSystemContext.startService(intentSerive);                setAlarmChange();            }            Slog.i("TEST", "End self-define alarm");        }    }

每天(或是周期性)固定的時間進行調用UpdateService的操作:

private void setAlarmChange(){        Slog.i(TAG, "Start self-define alarm");        manager = (AlarmManager) mSystemContext.getSystemService(ALARM_SERVICE);        Intent intent = new Intent();        intent.setComponent(new ComponentName("com.android.settings","com.android.settings.system.UpdateService"));        pi = PendingIntent.getService(mSystemContext,0,intent,0);        timedate = Settings.System.getString(mSystemContext.getContentResolver(),Settings.System.KEY_CHECK_TIME_VALUE);        dayset = Integer.parseInt(Settings.System.getString(mSystemContext.getContentResolver(), Settings.System.KEY_UPDATE_INTERVAL))+1;        temptimedate = timedate.split(":");        hourset = temptimedate[0];        mintuesset = temptimedate[1];        Calendar calendar = Calendar.getInstance();        calendar.setTimeInMillis(System.currentTimeMillis());        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourset));        calendar.set(Calendar.MINUTE, Integer.parseInt(mintuesset));        calendar.add(Calendar.DATE,dayset);        nextCheckAlarmTime = calendar.getTimeInMillis()+dayset*3600*24*1000;        Settings.System.putString(mSystemContext.getContentResolver(),Settings.System.KEY_NEXT_CHECK_TIME,String.valueOf(nextCheckAlarmTime));                // setRepeating() lets you specify a precise custom interval--in this case,        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),                dayset*3600*24*1000, pi);        Slog.i(TAG, "Start self-define alarm");    }

記得開啓Alarmagr前先清除之前的對象

private void cancelAlarmTime(){        if(manager!=null)        manager.cancel(pi);    }

 

更多相关文章

  1. android中使用线程(比如修改textview的text)
  2. Android(安卓)Studio Mac快捷键
  3. 【Android】Android设计准则
  4. 据9成手机操作系统份额的iOS和Android遇到了挑战者火狐
  5. Android进阶——阿里Android开发手册学习笔记(一)
  6. Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实
  7. Android应用在未启动的情况下无法收到指定广播的问题总结
  8. Android用户体验团队:Android(安卓)UI设计准则
  9. Android(安卓)小項目之---Toast對象詳細使用,兼溫習前內容(附源碼)

随机推荐

  1. Android init.rc解析
  2. Android百度地图之位置定位和附近查找代
  3. [置顶] Android多分辨率和多屏幕的布局适
  4. 安卓软硬结合,热点技术实践总结:《Android
  5. 浅谈android中的图片处理之基本绘图(一)
  6. 谁说Android的动画不廉价(五)之水波纹动画
  7. 【Android】多语言Values
  8. android之spinner背景、字体颜色
  9. Android动态加载jar/dex
  10. Android : 输入设备键值从底层到应用层的