java Android的简单报警管理器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android的简单报警管理器相关的知识,希望对你有一定的参考价值。

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.bhartiy.anywork.utils.Logcat;

public class NotifyAlaramManger {
	
	public static final int REQUEST_CODE = 111131;
	//private final static long INTERVAL=30*1000;
	//private final static long INTERVAL=7200000;
	public static final int FIVE_MINUTE = 300000;
	
	public static void setAlaram(Context context) {
		
		Calendar cal = Calendar.getInstance();

		//cal.add(Calendar.HOUR, 5);
		cal.add(Calendar.MINUTE, 5);

		long tomorrow = cal.getTimeInMillis();
		
		Intent intent = new Intent(context, NotificationReceiver.class);
		
		boolean alarmRunning = (PendingIntent.getBroadcast(context, REQUEST_CODE,intent,
				PendingIntent.FLAG_NO_CREATE) != null);
		
		//Check if alarm is already running 
		if(alarmRunning){
			Logcat.e("AlaramManager", "Alaram already running!");
			return;
		}
		PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC_WAKEUP,tomorrow,FIVE_MINUTE, sender);
		Logcat.e("alaram", "alaram set");
		//am.setRepeating(AlarmManager.RTC_WAKEUP,tomorrow,AlarmManager.INTERVAL_FIFTEEN_MINUTES, sender);
		
	}

	public static void updateAlaram(Context context) {
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, 5);

		long tomorrow = cal.getTimeInMillis();
		
		Intent intent = new Intent(context, NotificationReceiver.class);
		
		PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		
		am.setRepeating(AlarmManager.RTC_WAKEUP,tomorrow,FIVE_MINUTE, sender);

	}

	public static void cancelAlaram(Context context) {
		Intent intent = new Intent(context, NotificationReceiver.class);
		PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		am.cancel(sender);
	}
}

以上是关于java Android的简单报警管理器的主要内容,如果未能解决你的问题,请参考以下文章

安卓。报警管理器。重复警报不会触发,并且在一段时间后会立即触发所有警报

简单的线程管理 - Java - Android

报警管理器每日重复单词不正确

带有BroadcastReceiver的报警管理器

Android布局管理器-使用GridLayout表格布局管理器实现简单的用户登录页面

Android布局管理器-使用LinearLayout实现简单的登录窗口布局