广播机制:发送方不管接收方是否接收到数据,如何接收和处理
一、要想实现广播的接收,必须先创建一个类继承自BroadcastReceiver,复写其onReceive方法
二、在Manifest文件当中进行注册并设置action的过滤器,也可以在应用程序代码中注册
BroadcastReceiver的生命周期是,感兴趣的广播事件发生时创建对象,onReceive函数返回时销毁对象。
Intent 是action和data共同完成信息的携带的
如果BroadcastReceiver在Manifest中注册,则当应用程序关闭时,它依然会接收广播
<receiver android:name=".TestReceiver"> <intent-filter> <action android:name="android.intent.action.EDIT"/> </intent-filter> </receiver>
在代码中注册,用于更新UI,Activity启动时注册,不可见时取消注册
Android 系统中内置的BroadcastActions
这些内置的actions可以在帮助文档的Intent类的常量中查到
下面分别给出两个例子(在manifest文件中注册广播接收器和在代码中注册广播接收器)
注意如果在代码中注册,则必须先在manifest文件中设置权限
TestBCActivity.java:
package mars.testBC; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestBCActivity extends Activity { /** Called when the activity is first created. */ private Button sendButton; private Button registerButton = null; private Button unregisterButton = null; private SMSReceiver smsReceiver = null; private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendButton = (Button)findViewById(R.id.sendButton); sendButton.setOnClickListener(new SendListener()); registerButton = (Button)findViewById(R.id.registerButton); unregisterButton = (Button)findViewById(R.id.unregisterButton); registerButton.setOnClickListener(new RegisterListener()); unregisterButton.setOnClickListener(new UnRegisterListener()); } //例一 class SendListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub //生成Intent对象,挟带广播信息 Intent intent = new Intent(); //设置Action intent.setAction(Intent.ACTION_EDIT); //广播 TestBCActivity.this.sendBroadcast(intent); } } //例二 class RegisterListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub smsReceiver = new SMSReceiver(); //设置过滤器 IntentFilter filter = new IntentFilter(); //添加过滤事件(短消息接收事件) filter.addAction(SMS_ACTION); //注册广播接收器 TestBCActivity.this.registerReceiver(smsReceiver, filter); } } class UnRegisterListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub //解除绑定 TestBCActivity.this.unregisterReceiver(smsReceiver); } } }
TestReceiver.java:
package mars.testBC; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class TestReceiver extends BroadcastReceiver { public TestReceiver(){ System.out.println("TestReceive"); } @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub System.out.println("onReceive"); } }
SMSReceiver.java:
package mars.testBC; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; public class SMSReceiver extends BroadcastReceiver { SMSReceiver(){ System.out.println("SMSReceiver constructor"); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Bundle bundle = intent.getExtras(); Object[] myOBJpdus = (Object[])bundle.get("pdus"); SmsMessage[] messages = new SmsMessage[myOBJpdus.length]; System.out.println(messages.length); for(int i=0; i<myOBJpdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[])myOBJpdus[i]); System.out.println(messages[i].getDisplayMessageBody()); } } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/sendButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start"/> <Button android:id="@+id/registerButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定"/> <Button android:id="@+id/unregisterButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="解除绑定"/> </LinearLayout>
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mars.testBC" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".TestBCActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".TestReceiver"> <intent-filter> <action android:name="android.intent.action.EDIT"/> </intent-filter> </receiver> </application> <!-- 设置权限 --> <uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission> </manifest>