public class MyAlarmBroadcastReceiver extends BroadcastReceiver{
public static int count=0;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("test", "闹钟响铃了"+count++);
}
}
activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public long getTime(int second)
{
Calendar c=Calendar.getInstance();
c.add(Calendar.SECOND, second);
return c.getTimeInMillis();
}
public void clickBtn(View view)
{
int id=view.getId();
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
if(id==R.id.btn1)
{
//设定单次闹钟
Intent intent=new Intent();
intent.setAction("veryedu.cls229.lession18.ALARM_RING");
PendingIntent operation=PendingIntent.getBroadcast(this, 0x101, intent, 0);
//第一个参数是闹钟类型 WAKEUP 是当闹钟响铃时 手机会被唤醒
//第2个参数是触发时间 这里设定10秒后响铃
am.set(AlarmManager.RTC_WAKEUP, getTime(10), operation);
}else if(id==R.id.btn2)
{
//设定周期闹钟 即使你的应用程序关闭 闹钟的执行也不会受到影响
//闹钟的设定在关机(重启)后将会自动失效
//所以一般程序中闹钟的设定需要保存在sp文件中
//最好实现一个开机启动的服务 在这个服务中读取sp配置文件,把设定的闹钟恢复
Intent intent=new Intent();
intent.setAction("veryedu.cls229.lession18.ALARM_RING");
PendingIntent operation=PendingIntent.getBroadcast(this, 0x101, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,getTime(10),5*1000,operation);
}else if(id==R.id.btn3)
{
//停止闹钟 设定的参数是它取消的操作 由PendingIntent表示
//怎么区分这里的PI和上面的PI是同一个呢?
//PendingIntent实际是一个缓冲池 我们要执行的操作(intent)会放置在这里 缓存
//为了区分 每一个intent 需要设定requestCode 换句话说区分PendingIntent就是靠这个code
//所以这里想要取消成功 必须将requestCode设置成上面一样的
Intent intent=new Intent();
intent.setAction("veryedu.cls229.lession18.ALARM_RING");
PendingIntent operation=PendingIntent.getBroadcast(this, 0x101, intent, 0);
am.cancel(operation);
}else if(id==R.id.btn4)
{
// Intent intent=new Intent();
// intent.setAction("veryedu.cls229.lession18.ALARM_RING");
// PendingIntent operation=PendingIntent.getBroadcast(this, 0x101, intent, 0);
// am.set(AlarmManager.RTC_WAKEUP, getTime(5), operation);
// am.set(AlarmManager.RTC_WAKEUP, getTime(10), operation);
// am.set(AlarmManager.RTC_WAKEUP, getTime(15), operation);
// //在这种设定方式中 后一次设定会覆盖前一次设定
// am.set(AlarmManager.RTC_WAKEUP, getTime(20), operation);
Intent intent=new Intent();
intent.setAction("veryedu.cls229.lession18.ALARM_RING");
PendingIntent operation1=PendingIntent.getBroadcast(this, 0x101, intent, 0);
PendingIntent operation2=PendingIntent.getBroadcast(this, 0x102, intent, 0);
PendingIntent operation3=PendingIntent.getBroadcast(this, 0x103, intent, 0);
PendingIntent operation4=PendingIntent.getBroadcast(this, 0x104, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, getTime(5), operation1);
am.set(AlarmManager.RTC_WAKEUP, getTime(10), operation2);
am.set(AlarmManager.RTC_WAKEUP, getTime(15), operation3);
am.set(AlarmManager.RTC_WAKEUP, getTime(20), operation4);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<receiver android:name="com.alerm.MyAlarmBroadcastReceiver">
<intent-filter >
<action android:name="com8.ALARM_RING"/>
</intent-filter>
</receiver>
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="设定单次闹钟" />
<Button android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="设定周期闹钟" />
<Button android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="停止闹钟" />
<Button android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="设定重复闹钟" />
</LinearLayout>