Android AlarmManager

有时候需要定时调用某个任务或者隔一段时间触发一次事件,有一种思路是在线程中用定时器,但是有些手机系统休眠时会失效,针对这种方法有人提出用wakelock,虽然可以实现 ,但是比较消耗电量。Android提供的ArarmManager解决这个问题。

实现非常简单

1.指定PendingIntent

2.得到AlarmManager对象

3.启动


例子:

1.  3秒后启动一个servie

Service代码:

public class AlarmReceiveService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.v("alarm", "--------------------");
		return super.onStartCommand(intent, flags, startId);
	}
}

MainActivity代码"

alarmOnece(MainActivity.this, 3000);
	void alarmOnece(Context mContext, int times){
    	Intent intent = 
        		new Intent(mContext, AlarmReceiveService.class);

        	PendingIntent pi = 
        		PendingIntent.getService(
        		  mContext, 	//context
                  1, 		    //request id
                  intent, 	    //intent 
                  PendingIntent.FLAG_ONE_SHOT);  //pending intent flags
        	// 设置alarm
        	AlarmManager am = 
        		(AlarmManager)
        		     mContext.getSystemService(Context.ALARM_SERVICE);
        	
        	am.set(AlarmManager.RTC_WAKEUP, 
        			times, 
        			pi);
	}
3秒后可以看到logcat的输出。


2.设置重复的Alarm

3秒后启动alarm,每隔3秒触发一次启动service的操作

alarmRepeat(MainActivity.this, 3000);

	void alarmRepeat(Context mContext, int times){
		Intent intent = 
				new Intent(mContext, AlarmReceiveService.class);
		
		PendingIntent pi = 
				PendingIntent.getService(
						mContext, 	//context
						1, 		    //request id
						intent, 	    //intent 
						0);  //pending intent flags
		// 设置alarm
		AlarmManager am = 
				(AlarmManager)
				mContext.getSystemService(Context.ALARM_SERVICE);
		
		am.setRepeating(AlarmManager.RTC_WAKEUP, 
				times, //几秒后开始
				times, //几秒后重复
				pi);
	}
即使应用程序退出,alarmManager依然会执行,除非关机。

要在程序中退出,可以这样:

	void cancelAlarm(Context mContext, int requestId){
		Intent intent = 
				new Intent(mContext, AlarmReceiveService.class);
		
		PendingIntent pi = 
				PendingIntent.getService(
						mContext, 	//context
						requestId, 		    //request id
						intent, 	    //intent 
						0);  //pending intent flags

		AlarmManager am = 
				(AlarmManager)
				mContext.getSystemService(Context.ALARM_SERVICE);

		am.cancel(pi);
	}

注意requestId要和之前alarmRepeat方法中设置的pengdingIntent的requestId一致

3.多个Alarm

执行多个alarm要根据requestId区分

	void alarmMulti(Context mContext, int times){
    	Intent intent = 
        		new Intent(mContext, AlarmReceiveService.class);

        	PendingIntent pi = getDistinctPendingIntent(mContext, intent, 1);
        	PendingIntent pi2 = getDistinctPendingIntent(mContext, intent, 2);
        	PendingIntent pi3 = getDistinctPendingIntent(mContext, intent, 3);
        	// 设置alarm
        	AlarmManager am = 
        		(AlarmManager)
        		     mContext.getSystemService(Context.ALARM_SERVICE);
        	
        	am.set(AlarmManager.RTC_WAKEUP, 
        			times, 
        			pi);
        	am.set(AlarmManager.RTC_WAKEUP, 
        			times, 
        			pi2);
        	am.set(AlarmManager.RTC_WAKEUP, 
        			times, 
        			pi3);
	}

你可能感兴趣的:(Android AlarmManager)