Android Alarm 的设置

编程入门 行业动态 更新时间:2024-10-20 16:44:46

<a href=https://www.elefans.com/category/jswz/34/1771384.html style=Android Alarm 的设置"/>

Android Alarm 的设置

一、闹钟的分类

1)从闹钟的设置时间方式分为:以开启启动后的间隔时间和日历时间 2)从硬件上来说分为:1、当cpu休眠时不启动 2、即时cpu休眠时仍然启动 所以有如下四种:
  • ELAPSED_REALTIME—Fires the pending intent based on the amount of time since the device was booted, but doesn't wake up the device. The elapsed time includes any time during which the device was asleep.
  • ELAPSED_REALTIME_WAKEUP—Wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.
  • RTC—Fires the pending intent at the specified time but does not wake up the device.
  • RTC_WAKEUP—Wakes up the device to fire the pending intent at the specified time

如何设置闹钟

1)设置ELAPSED闹钟
privateAlarmManager alarmMgr;
privatePendingIntent alarmIntent;
...
alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent =newIntent(context,AlarmReceiver.class);
alarmIntent =PendingIntent.getBroadcast(context,0, intent,0);

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime()+
        60*1000, alarmIntent);
2)设置RTC
Calendar calendar =Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);
若需要精确的时间则可以设置:setRepeating()


如何使闹钟开机启动

1)设置开机启动
<uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
2)编写Receiver并在Receiver中设置开启启动的Action
<receiverandroid:name=".SampleBootReceiver"
        android:enabled="false">
    <intent-filter>
        <actionandroid:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
3)通过设置packmange的setComponentEnabledSetting属性开启
ComponentName receiver =newComponentName(context,SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);
这样即使重启闹钟依然有效

更多推荐

Android Alarm 的设置

本文发布于:2024-02-06 22:43:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   Alarm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!