以下是我利用AlarmManager+BroadcastReceiver实现闹钟的功能,以及通过广播来实现通知,这里我的闹铃是存放在内存raw中,并不需要在清单文件中获取权限
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟(一次)"
android:onClick="setAlarmOne"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setAlarmCycle"
android:text="设置闹钟(周期)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消周期闹钟"
android:onClick="cancelAlarmCycle"
/>
</LinearLayout>
MainActivity.java
package com.exa