本实例介绍自定义Broadcast Receiver和系统
1、自己定义Broadcast Receiver来处理广播事件
自己定义一个广播是比较简单的,首先在我们的程序组件里构建想要广播的Intent,使用sendBroadcast方法发送出去。其次定义一个广播接收器,该广播接收器继承BroadcastReceiver,并且覆盖onReceive()方法来相应事件。
最后注册该广播接收器,我们可以在代码中注册,也可以在AndroidManifest.xml配置文件中注册。
下面通过一个实例来演示Intent发出广播和Broadcast Receiver处理广播的过程:
MyReceive.java
public class MyReceive extends BroadcastReceiver{
/* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
在AndroidManifest配置文件中声明广播接收器组件:
<receiver android:name="MyReceive">
<intent-filter >
<action android:name = "com.newcosoft.receive.MY_ACTION"></action>
</intent-filter>
</receiver>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="发送广播"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private Button sendButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendButton = (Button) this.findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//实例化Intent对象
Intent intent = new Intent();
//设置Intent action的属性
intent.setAction("com.newcosoft.receive.MY_ACTION");
intent.putExtra("message", "土豆土豆,我是地瓜,收到请回答,收到请回答…");
//发出广播
sendBroadcast(intent);
}
});
}
}
运行效果图:
2、系统广播事件的使用
除了上面我们自己定义的广播时间,Android系统还提供了一些自带的标准广播Action;这些广播是由系统自动发出的,我们只要直接接收即可。
系统自带标准广播action常量如下:
常量名称 | 常量值 | 意义 |
ACTION_BOOT_COMPLETED | android.intent.action. BOOT_COMPLETED | 系统启动完成 |
ACTION_TIME_CHANGED | android.intent.action. TIME_CHANGED | 时间改变 |
ACTION_DATE_CHANGED | android.intent.action. DATE_CHANGED | 日期改变 |
ACTION_TIMEZONE_CHANGED | android.intent.action. TIMEZONE_CHANGED | 时区改变 |
ACTION_BATTERY_LOW | android.intent.action. BATTERY_LOW | 电量低 |
ACTION_MEDIA_EJECT | android.intent.action. MEDIA_EJECT | 插入或拔出外部媒体 |
ACTION_MEDIA_BUTTON | android.intent.action. MEDIA_BUTTON | 按下媒体按钮 |
ACTION_PACKAGE_ADDED | android.intent.action. PACKAGE_ADDED | 添加包 |
ACTION_PACKAGE_REMOVED | android.intent.action. PACKAGE_REMOVED | 删除包 |
对于具体怎么运用,直接上代码:
public class MyReceiver2 extends BroadcastReceiver{
/* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MyReceiver2", "SUCCESS!!!");
Toast.makeText(context, "SUCCESS!!!", Toast.LENGTH_LONG).show();
}
}
配置文件:
<receiver android:name="MyReceiver2">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
重新启动模拟器,在LogCat中会输出:
我们既可以在AndroidManifest中注册一个广播接收器,也可以通过代码的方式来注册。当然我们也可以注销一个广播接收器。一般我们是在Activity.onResume()方法中使用registerReceiver()方法来注册一个广播接收器,在Activity.onPause()中使用unregisterReceiver(r)来注销一个广播接收器。下面的代码片段显示了如何使用一个intentFilter注册Broadcast Receiver。
IntentFilter filter = new IntentFilter();
MyReceiver2 r = new MyReceiver2();
//注册Receiver
registerReceiver(r,filter);