Android提高第六篇之BroadcastReceiver
前面分别讨论了html" target=_blank>Activity和Service,这次就轮到BroastcastReceiver,Broastcast是应用程序间通信的手段。BroastcastReceiver也是跟Intent紧密相连的,动态/静态注册了BroastcastReceiver之后,使用sendBroadcast把Intent发送之后,系统会自动把符合条件的BroastcastReceiver启动,跟嵌入式系统的中断类似。
本文主要演示了如何静态/动态注册BroastcastReceiver,向系统索取电量信息,以及枚举信息的字段。本文运行截图如下:
上图是发送Intent至内部动态注册的BroadcastReceiver,接收到之后显示消息名称。动态注册BroadcastReceiver用到registerReceiver()。
上图是发送Intent至内部静态注册的BroadcastReceiver,接收到之后显示消息名称。静态注册比动态注册麻烦点,先新建一个类继承BroadcastReceiver,然后到AndroidManifest.xml 添加
view plaincopy to clipboardprint?
<receiver android:name="clsReceiver2">
<intent-filter>
<action
android:name="com.testBroadcastReceiver.Internal_2"/>
</intent-filter>
</receiver>
<receiver android:name="clsReceiver2">
<intent-filter>
<action
android:name="com.testBroadcastReceiver.Internal_2"/>
</intent-filter>
</receiver>第一个name是类名,第二个是action的名称。
上图是枚举Intent消息的字段,这个功能比较适合懒人,把收到的Intent消息的字段全部分解了,再看看哪个需要的,懒得记住。实现这部分的代码如下:
view plaincopy to clipboardprint?
//当未知Intent包含的内容,则需要通过以下方法来列举
Bundle b=intent.getExtras();
Object[] lstName=b.keySet().toArray();
for(int i=0;i<lstName.length;i++)
{
String keyName=lstName[i].toString();
Log.e(keyName,String.valueOf(b.get(keyName)));
}
//当未知Intent包含的内容,则需要通过以下方法来列举
Bundle b=intent.getExtras();
Object[] lstName=b.keySet().toArray();for(int i=0;i<lstName.length;i++)
{
String keyName=lstName[i].toString();
Log.e(keyName,String.valueOf(b.get(keyName)));
}main.xml的代码如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<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/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送至内部动态注册的BroadcastReceiver"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送至内部静态注册BroadcastReceiver"></Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送至系统BroadcastReceiver"></Button>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送至内部动态注册的BroadcastReceiver"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送至内部静态注册BroadcastReceiver"></Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送至系统BroadcastReceiver"></Button>
</LinearLayout>
testBroadcastReceiver.java的代码如下:
view plaincopy to clipboardprint?
package com.testBroadcastReceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class testBroadcastReceiver extends Activity {
Button btnInternal1,btnInternal2,btnSystem;
static final String INTENAL_ACTION_1 = "com.testBroadcastReceiver.Internal_1";
static final String INTENAL_ACTION_2 = "com.testBroadcastReceiver.Internal_2";
static final String INTENAL_ACTION_3 = "com.testBroadcastReceiver.Internal_3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnIn
补充:移动开发 , Android ,