Android AlarmManager报警的实现

什么是AlarmManager?

AlarmManager它是Android经常使用的系统-Level提醒服务,我们指定为广播中的特定时间Intent。

我们设定一个时间,然后在该时间到来时。AlarmManager为我们广播一个我们设定的Intent,通常我们使用 PendingIntent,PendingIntent能够理解为Intent的封装包,简单的说就是在Intent上在加个指定的动作。在使用Intent的时候,我们还须要在运行startActivity、startService或sendBroadcast才干使Intent实用。而PendingIntent的话就是将这个动作包括在内了

定义一个PendingIntent对象

PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);

AlarmManager的经常使用API

1、set(int type,long startTime。PendingIntent pi)

该方法用于设置一次性闹钟,第一个參数表示闹钟类型,第二个參数表示闹钟运行时间。第三个參数表示闹钟响应动作。

2、setRepeating(int type,long startTime,long intervalTime,PendingIntent pi)

该方法用于设置反复闹钟。第一个參数表示闹钟类型。第二个參数表示闹钟首次运行时间。第三个參数表示闹钟两次运行的间隔时间。第三个參数表示闹钟响应动作。


3、setInexactRepeating(int type。long startTime,long intervalTime,PendingIntent pi)

该方法也用于设置反复闹钟。与第二个方法相似,只是其两个闹钟运行的间隔时间不是固定的而已。

參数具体解释:

int type

闹钟的类型,经常使用的有5个值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。


larmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动開始),状态值为3;
AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并运行提示功能。该状态下闹钟也使用相对时间,状态值为2;
AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间。状态值为1;
AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并运行提示功能,该状态下闹钟使用绝对时间,状态值为0;
AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之中的一个。该状态下闹钟也是用绝对时间,状态值为4。只是本状态好像受SDK版本号影响。某些版本号并不支持;

long startTime

闹钟的第一次运行时间,以毫秒为单位,能够自己定义时间,只是一般使用当前时间。

须要注意的是,本属性与第一个属性(type)密切相关,假设第一个參数对 应的闹钟使用的是相对时间(ELAPSED_REALTIME和ELAPSED_R EALTIME_WAKEUP),那么本属性就得使用相对时间(相对于 系统启动时间来说)。比方当前时间就表示为:SystemClock.elapsedRealtime()。假设第一个參数相应的闹钟使用的是绝对时间 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比方当前时间就表示 为:System.currentTimeMillis()。

long intervalTime

对于后两个方法来说,存在本属性,表示两次闹钟运行的间隔时间。也是以毫秒为单位。


PendingIntent pi

绑定了闹钟的运行动作,比方发送一个广播、给出提示等等。

PendingIntent是Intent的封装类。须要注意的是,假设是通过启动服务来实现闹钟提 示的话,PendingIntent对象的获取就应该採用Pending.getService(Context c,int i,Intent intent,int j)方法;假设是通过广播来实现闹钟提示的话。PendingIntent对象的获取就应该採用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法。假设是採用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该採用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。假设这三种方法错用了的话,尽管不会报错,可是看不到闹钟提示效果。

写一个简单的Demo

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建Intent对象,action为ELITOR_CLOCK,附加信息为字符串“你该打酱油了”
        Intent intent = new Intent("ELITOR_CLOCK");
        intent.putExtra("msg", "你该打酱油了");

    //定义一个PendingIntent对象。PendingIntent.getBroadcast包括了sendBroadcast的动作。

//也就是发送了action 为"ELITOR_CLOCK"的intent PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); //AlarmManager对象,注意这里并非new一个对象,Alarmmanager为系统级服务 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); //设置闹钟从当前时间開始,每隔5s运行一次PendingIntent对象pi,注意第一个參数与第二个參数的关系 // 5秒后通过PendingIntent pi对象发送广播 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5 * 1000, pi); } }

广播接收者:MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.d("MyTag", "onclock......................");
        String msg = intent.getStringExtra("msg");
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}
清单文件例如以下:AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarmdemo" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="ELITOR_CLOCK" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
执行结果:


下载Demo请猛戳



版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/mfrbuaa/p/4887278.html