AlarmManager的使用心得

本文属于原创,转载请注明出处。
最近在做一个项目。里面有用到了AlarmManager这个类,这里就讲一讲使用AlarmManager过程中遇到的问题以及心得。

1.AlarmManager的概述

AlarmManager是安卓手机中用来开发闹钟的一个类,通常作为全局的一个定时器来定时启动某段代码。

2. 和Timer类的区别

Timer类也是安卓开发中常用到的一个定时器类,但是Timer只能在手机运行的时候使用,也就是说在手机休眠(黑屏)的情况下是没有作用的,但是AlarmManager是系统的一个类,它能在手机休眠的情况下也能够运行。

3. AlarmManager的使用

设置定时任务有以下几种方法:

 - set(int type,long startTime,PendingIntent pi):一次性闹钟
 - setRepeating(int type,long startTime,long intervalTime,PendingIntent pi): 重复性闹钟,和3有区别,3闹钟间隔时间不固定
 - setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi): 重复性闹钟,时间不固定

但是由于安卓系统的升级,谷歌为了更好地管理手机电量,减少唤醒系统,使得以上方法设定的时间不够准确,因此参考官网给出的提示,给出了更为有效的方法,但是在实际测试中还是有很多的问题。

针对设置单次闹钟的方法

在SDK_INT 19以后推荐使用

 - setExact(int type, long triggerAtMillis, PendingIntent operation)
 - setWindow(int type, long windowStartMillis, long windowLengthMillis,PendingIntent operation)

在SDK_INT 23以后推荐使用

- setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)

这样设置单次闹钟精度就可以做到比较准确了

针对设置重复闹钟的方法

在SDK_INT 19以前就使用setReapting()方法,在SDK_INT 19以后官方并没有给出有效的解决方法,因此可以使用取巧的方法,就是循环设置准确闹钟,来达到设置重复闹钟的目的。
举例:
第一次设置闹钟

- setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)

然后在该闹钟的接受器里面(无论是Broadcast,还是Service,或者是Activity)再设置一遍,注意前后闹钟的id要一致,后面具体会给出一个demo来。

 - setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)

闹钟type的类型

 - AlarmManager.ELAPSED_REALTIME:使用相对时间,可以通过SystemClock.elapsedRealtime()
   获取(从开机到现在的毫秒数,包括手机的睡眠时间),设备休眠时并不会唤醒设备。
 - AlarmManager.ELAPSED_REALTIME_WAKEUP:与ELAPSED_REALTIME基本功能一样,只是会在设备休眠时唤醒设备。
 - AlarmManager.RTC:使用绝对时间,可以通过System.currentTimeMillis()获取,设备休眠时并不会唤醒设备。
 - AlarmManager.RTC_WAKEUP: 与RTC基本功能一样,只是会在设备休眠时唤醒设备。

取消闹钟

 - cancel(PendingIntent operation)

AlarmManager的具体使用
AlarmManager可以使用Activity,Broadcast,Service来接受这个闹钟,并执行代码,这里以Broadcast来举个栗子
声明一个action常量

public static final String ALARM_SINGLE_ACTION = "com.example.heavn.ireminder.ALARM_SINGLE_ACTION";

设置一个闹钟

Intent intent = new Intent(context,AlarmReceiver.class);
intent.setAction(ALARM_SINGLE_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(context, clockId, intent,0);
//设置一个PendingIntent对象,发送广播
AlarmManager am = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
//根据不同的api调用不同的方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,time,pi);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    am.setExact(AlarmManager.RTC_WAKEUP,time,pi);
}else{
    am.set(AlarmManager.RTC_WAKEUP,time,pi);
}

在这里,PendingIntent.getBroadcast()代表发送广播,会在广播接收器AlarmReceiver当中接收到,然后执行命令,除此之外,PendingIntent.getServicet()会在服务当中收到,PendingIntent.getActivity()会在活动当中收到;
clockId代表的是闹钟的标识符,用来区别不同的闹钟

AlarmReceiver.class

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals(ALARM_SINGLE_ACTION)){
            Log.e("type","单次");
        }
}

然后要记得注册广播,我在这里使用了静态注册,在Androidmanifest.xml文件当中声明

<receiver android:name=".AlarmReceiver"
	  android:enabled="true"
	  android:exported="true">
	  <intent-filter>
	      <action android:name="com.example.heavn.ireminder.ALARM_SINGLE_ACTION"/>
	  </intent-filter>
</receiver>

这样一个闹钟就可以运行了

取消一个闹钟

Intent intent = new Intent(ALARM_SINGLE_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(context, clockId, intent,0);
 //设置一个PendingIntent对象,发送广播
 AlarmManager am = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
 am.cancel(pi);

4.在AlarmManager当中遇到的那些坑

唤醒屏幕

很多时候使用AlarmManager就是想要在固定时刻执行某段代码,弹出一些view,但是手机处于休眠状态是无效的,这时候就需要唤醒屏幕:
这里使用服务来唤醒屏幕

public class AlarmService extends Service {
    // 键盘管理器
    KeyguardManager mKeyguardManager;
    // 键盘锁
    private KeyguardLock mKeyguardLock;
    // 电源管理器
    private PowerManager mPowerManager;
    // 唤醒锁
    private PowerManager.WakeLock mWakeLock;
    private MyApp app;

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("a","----> 开启服务");
        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.e("a","----> 开始服务");
        // 点亮亮屏
        mWakeLock = mPowerManager.newWakeLock
                (PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag");
        mWakeLock.acquire();
        // 初始化键盘锁
        mKeyguardLock = mKeyguardManager.newKeyguardLock("unlock");
        // 键盘解锁
        mKeyguardLock.disableKeyguard();

		//以下是执行自己的想要执行的代码,这里是打开一个新的活动
        Alarm alarm = (Alarm)intent.getSerializableExtra("alarm");
        Intent activityIntent = new Intent(this, LockScreenActivity.class);
        activityIntent.putExtra("alarm",alarm);
        //新开一个intent
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(activityIntent);

    }
    //一定要释放唤醒锁和恢复键盘
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWakeLock != null) {
            Log.e("a","----> 终止服务,释放唤醒锁");
            mWakeLock.release();
            mWakeLock = null;
        }
        if (mKeyguardLock!=null) {
            Log.e("a","----> 终止服务,重新锁键盘");
            mKeyguardLock.reenableKeyguard();
        }
    }

}

当然要注意声明权限,静态和动态声明都要记得,以及在Androidmanifest.xml当中声明这个服务

<!-- 唤醒手机和解锁键盘 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

当然,如果你的键盘锁加了密码是无法直接打开屏幕跳出相应的活动的。

<service android:name=".AlarmService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.heavn.ireminder.ALARM_SINGLE_ACTION"/>
    </intent-filter>
</service>

然后在AlarmReceiver里面开启这个活动

//跳转到服务
Intent serviceIntent = new Intent(context, AlarmService.class);
context.startService(serviceIntent);

AlarmManager的intent无法传递数据

在设置闹钟的时候,使用intent来传递数据,结果在API27的手机上测试,发现除了String类型的数据可以传递到AlarmReceiver上能够接收到之外,出现了其他数据类型接收不到的情况。而在API23的手机上完全没有这些问题,至于其他API的手机暂未进行设置。
针对这种情况,可以使用Bundle来解决
用intent来传一个Alarm的对象
记得Alarm类要implements Serializable ,将其序列化之后才能传递
在设置闹钟的时候

//使用bundle在intent当中传对象,能够把数据传过去,否则api27的无法成功传数据
Bundle bundle = new Bundle();
bundle.putSerializable("alarm",alarm);

intent.putExtra("data",bundle);

然后取出bundle数据

// 实例化一个Bundle,使用Bundle接收数据,否则api27以上表示接收不到
Bundle bundle = intent.getBundleExtra("data");
Alarm alarm = (Alarm)bundle.getSerializable("alarm");

这样就成功解决了无法使用intent将其他类型数据传递过去的问题

循环设置闹钟

循环设置闹钟的时候一定要注意闹钟的id一定要不同,否则相同id的闹钟会被覆盖,只剩下最后一次设置的闹钟才会执行

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。首先需要了解一下 AlarmManager 是什么。AlarmManagerAndroid 系统提供的一种定时服务,可以在指定的时间点或者间隔时间到达时,执行指定的操作。而 AppWidget 是 Android 系统提供的一种小部件,可以在桌面上显示应用程序的一些信息或操作。 为了实现 AppWidget 的实时更新,我们可以使用 AlarmManager 来定时触发更新操作。下面是注册 AlarmManager 的步骤: 1. 获取 AlarmManager 实例: ```java AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); ``` 2. 创建 PendingIntent 对象: ```java Intent intent = new Intent(this, UpdateWidgetService.class); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); ``` 其中,UpdateWidgetService 是我们自定义的服务,用来更新 AppWidget 的内容。 3. 设置 AlarmManager 的触发时间和重复间隔: ```java long intervalMillis = 30 * 60 * 1000; // 30 分钟 long triggerAtMillis = System.currentTimeMillis() + intervalMillis; alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, pendingIntent); ``` 这里的 setRepeating() 方法会在 triggerAtMillis 时间点开始,每隔 intervalMillis 时间重复触发 PendingIntent 对象。 需要注意的是,AppWidget 的更新间隔时间应该适当,过短会导致电量消耗过大,过长则不能及时更新,影响用户体验。建议根据实际需求来设置更新间隔时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值