Android闹钟 AlarmManager的使用

targetSdkVersion在API 19之前应用仍将继续使用以前的行为,所有的闹钟在要求准确传递的情况下都会准确传递。

闹钟Demo


Android Api demos中就有关于闹钟使用的Demo:

com.example.android.apis.app.AlarmController

其中设定了两种闹钟,一种是一次性的,一种是重复的。

Manifest中的声明,process属性

自定义的receiver,在manifest中声明如下:

复制代码


        <receiver

            android:name\=".OneShotAlarm"

            android:process\=":remote" />

        <receiver

            android:name\=".RepeatingAlarm"

            android:process\=":remote" />

复制代码

Demo中两个Receiver的onReceive方法中显示了各自的Toast提示,所以不再列出。

在此讨论一下process属性,它规定了组件(activity, service, receiver等)所在的进程。

通常情况下,没有指定这个属性,一个应用所有的组件都运行在应用的默认进程中,进程的名字和应用的包名一致。

比如manifest的package=“com.example.helloalarm”,则默认进程名就是com.example.helloalarm。

****元素的process属性可以为全部的组件设置一个不同的默认进程。

组件可以override这个默认的进程设置,这样你的应用就可以是多进程的。

如果你的process属性以一个冒号开头,进程名会在原来的进程名之后附加冒号之后的字符串作为新的进程名。当组件需要时,会自动创建这个进程。这个进程是应用私有的进程。

如果process属性以小写字母开头,将会直接以属性中的这个名字作为进程名,这是一个全局进程,这样的进程可以被多个不同应用中的组件共享。

一次性闹钟

复制代码


            // When the alarm goes off, we want to broadcast an Intent to our

            // BroadcastReceiver. Here we make an Intent with an explicit class

            // name to have our own receiver (which has been published in

            // AndroidManifest.xml) instantiated and called, and then create an

            // IntentSender to have the intent executed as a broadcast.

            Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);

            PendingIntent sender \= PendingIntent.getBroadcast(

                    AlarmController.this, 0, intent, 0);



            // We want the alarm to go off 10 seconds from now.

            Calendar calendar = Calendar.getInstance();

            calendar.setTimeInMillis(System.currentTimeMillis());

            calendar.add(Calendar.SECOND, 10);



            // Schedule the alarm!

            AlarmManager am = (AlarmManager) getSystemService(ALARM\_SERVICE);

            am.set(AlarmManager.RTC\_WAKEUP, calendar.getTimeInMillis(), sender);

复制代码

重复闹钟

闹钟设置:

复制代码


            // When the alarm goes off, we want to broadcast an Intent to our

            // BroadcastReceiver. Here we make an Intent with an explicit class

            // name to have our own receiver (which has been published in

            // AndroidManifest.xml) instantiated and called, and then create an

            // IntentSender to have the intent executed as a broadcast.

            // Note that unlike above, this IntentSender is configured to

            // allow itself to be sent multiple times.

            Intent intent = new Intent(AlarmController.this,

                    RepeatingAlarm.class);

            PendingIntent sender \= PendingIntent.getBroadcast(

                    AlarmController.this, 0, intent, 0);



            // We want the alarm to go off 10 seconds from now.

            Calendar calendar = Calendar.getInstance();

            calendar.setTimeInMillis(System.currentTimeMillis());

            calendar.add(Calendar.SECOND, 10);

            // Schedule the alarm!

            AlarmManager am = (AlarmManager) getSystemService(ALARM\_SERVICE);

            am.setRepeating(AlarmManager.RTC\_WAKEUP,

                    calendar.getTimeInMillis(), 10 \* 1000, sender);

复制代码

闹钟取消:

复制代码


            // Create the same intent, and thus a matching IntentSender, for

            // the one that was scheduled.

            Intent intent = new Intent(AlarmController.this,

                    RepeatingAlarm.class);

            PendingIntent sender \= PendingIntent.getBroadcast(

                    AlarmController.this, 0, intent, 0);



            // And cancel the alarm.

            AlarmManager am = (AlarmManager) getSystemService(ALARM\_SERVICE);

            am.cancel(sender);

复制代码

AlarmManager说明


AlarmManager这个类提供对系统闹钟服务的访问接口。

对它的获取是通过系统服务:

Context.getSystemService(Context.ALARM_SERVICE)

相关方法说明:

cancel(PendingIntent operation)方法将会取消Intent匹配的任何闹钟。

关于Intent的匹配,查看filterEquals(Intent other)方法的说明可知,两个Intent从intent resolution(filtering)(Intent决议或过滤)的角度来看是一致的,即认为两个Intent相等。即是说,Intent的action,data,type,class,categories是相同的,其他的数据都不在比较范围之内。

set(int type, long triggerAtMillis, PendingIntent operation)方法将会设置一个闹钟。

注意:对于计时操作,可能使用Handler更加有效率和简单。

设置闹钟的时候注意:

最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

以下是今天给大家分享的一些独家干货:

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

天给大家分享的一些独家干货:

[外链图片转存中…(img-w5Eqql0z-1714464486905)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 25
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值