新建一个class 继承BroadcastReceiver,并实现onReceive()方法。当BroadcastReceiver接收到广播后,就会去执行OnReceive()方法。所以,我们在OnReceive()方法中加上代码,当接收到广播后就跳到显示提醒信息的Activity。具体代码如下( MyReceiver.java):
package com.Reminder;
import android.content.BroadcastReceiver;import android.content.Context;
import android.content.Intent;
/**
* Receive the broadcast and start the activity that will show the alarm
*/
public class MyReceiver extends BroadcastReceiver {
/**
* called when the BroadcastReceiver is receiving an Intent broadcast.
*/
@Override
public void onReceive(Context context, Intent intent) {
/* start another activity - MyAlarm to display the alarm */
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, MyAlarm.class);
context.startActivity(intent);
}
}
注意:创建完BroadcastReceiver后,需要在AndroidManifest.xml中注册:
<receiver android:name=".MyReceiver">