Android中的Alarm
16lz
2021-12-04
android中的定时任务。由AlarmManager对象进行管理
1.定时唤醒广播接受者
直接贴代码,在代码中说明
AlarmController.java
然后可以定期执行OneShotAlarm和RepeatingAlarm中的onRecevier中的内容
2.1.定时创建service
关键代码:
完整例子:
1.定时唤醒广播接受者
直接贴代码,在代码中说明
AlarmController.java
public class AlarmController extends Activity{private Toast mToast;private static final String TAG = "app";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.alarm_controller);Button button = (Button)findViewById(R.id.one_shot);button.setOnClickListener(oneShotListener);button = (Button)findViewById(R.id.start_repeating);button.setOnClickListener(startRepeatingListener);button = (Button)findViewById(R.id.stop_repeating);button.setOnClickListener(stopRepeatingListener);}private OnClickListener oneShotListener = new OnClickListener() {@Overridepublic void onClick(View v) {//只进行一次定时,不重复执行AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.SECOND, 30);//设置30秒后执行/* * 根据PendingIntent.getBroadcast * 指明定时唤醒广播接收者 * */PendingIntent pending = PendingIntent.getBroadcast(AlarmController.this, 0, new Intent(AlarmController.this,OneShotAlarm.class), 0);am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending); //表示在指定时间后执行if(mToast!=null){mToast.cancel();}mToast.makeText(AlarmController.this, "one_shot_scheduled", Toast.LENGTH_LONG).show();}};private OnClickListener startRepeatingListener = new OnClickListener() {@Overridepublic void onClick(View v) {//重复执行AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);PendingIntent pend = PendingIntent.getBroadcast(AlarmController.this, 0, new Intent(AlarmController.this,RepeatingAlarm.class), 0);long triggerAtTime = SystemClock.elapsedRealtime();triggerAtTime +=15*1000;//表示第一次执行15秒后am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, 15*1000, pend);//表示过15秒重复唤醒广播接受者if(mToast!=null){mToast.cancel();}mToast.makeText(AlarmController.this, "repeating_scheduled", Toast.LENGTH_LONG).show();}};private OnClickListener stopRepeatingListener = new OnClickListener() {@Overridepublic void onClick(View v) {//取消alarmAlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);PendingIntent pend = PendingIntent.getBroadcast(AlarmController.this, 0, new Intent(AlarmController.this,RepeatingAlarm.class), 0);am.cancel(pend);if(mToast!=null){mToast.cancel();}mToast.makeText(AlarmController.this, "stop_repeating_scheduled", Toast.LENGTH_LONG).show();}};}
然后可以定期执行OneShotAlarm和RepeatingAlarm中的onRecevier中的内容
2.1.定时创建service
关键代码:
//注意此方法PendingIntent.getService()PendingIntent pend = PendingIntent.getService(this, 0, new Intent(this,AlarmService_Service.class), 0);AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);long firstTime = SystemClock.elapsedRealtime();am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, pend);
完整例子:
更多相关文章
- [置顶] [Android] AsyncTask使用实例---加载网络图片
- android 使用gdb调试的方式
- 浅析android下如何通过jni监控wifi网络连接、dhcpcd执行和power
- Android上传文件到Web服务器,PHP接收文件(二)
- FregServer进程,发送BC_TRANSACTION,唤醒ServiceManager进程,返回BR
- Android(安卓)ADB(Android(安卓)Debug Bridge)简单使用
- Android(安卓)SDK Emulator: Compile CyanogenMod
- Android运行底层linux外部命令的实现
- Android(安卓)单元测试 Robolectric