标题
偶然间翻到公司的祖传代码,发现AlarmManager这个东西,原来确实没用过,找了点资料自己学习了下,在这记录下
主AlarmManagerActivity 代码
package com.jph.simple;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.jph.simple.service.AlarmService;
import java.util.Calendar;
/**
* Created by wyb on 2018/2/24.
* <p>
* FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
* <p>
* FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
* <p>
* FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
* <p>
* FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras
*/
public class AlarmManagerActivity extends Activity {
private AlarmManager alManager;
private AlarmReceiver alarmReceiver;
/******receiver_action*******/
public final static String ALARM_RECEIVER_ACTION = "receiver_alarm_action";
/******activity_action*******/
public final static String ALARM_ACTIVITY_ACTION = "activity_alarm_action";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarmmanager_layout);
alManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// System.out.println("----getLocalClassName---------" + getLocalClassName());
// System.out.println("----getPackageName---------" + getPackageName());
// System.out.println("----getComponentName---------" + getComponentName());
// System.out.println("----getCallingPackage---------" + getCallingPackage());
// System.out.println("----getTitle---------" + getTitle());
/***********alarmManager广播注册*************/
alarmReceiver = new AlarmReceiver();
registerReceiver(alarmReceiver,new IntentFilter(ALARM_RECEIVER_ACTION));
}
/**
* 开启定时任务
* @param view
*/
public