绑定广播有两种方式
一.配置文件绑定,在程序未启动也能监听
二.代码方式绑定,在程序启动后才能监听
1.绑定和取消绑定广播
public class MainActivity extends Activity { private Button registerButton=null; private Button unRegisterButton=null; private SMSReceiver smsReceiver=null; private static final String SMS_ACTION="android.provider.Telephony.SMS_RECEIVED"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerButton=(Button)findViewById(R.id.registerButton); unRegisterButton=(Button)findViewById(R.id.unRegisterButton); registerButton.setOnClickListener(new RegisterReceiverListener()); unRegisterButton.setOnClickListener(new UnRegisterReceiverListener()); /*if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); }*/ } class RegisterReceiverListener implements OnClickListener { @Override public void onClick(View v) { System.out.println("registerReceiver"); //生成一个BroadcastReceiver对象 smsReceiver=new SMSReceiver(); //生成一个IntentFilter对象 IntentFilter filter=new IntentFilter(); //为IntentFilter添加一个Action filter.addAction(SMS_ACTION); MainActivity.this.registerReceiver(smsReceiver, filter); } } class UnRegisterReceiverListener implements OnClickListener { @Override public void onClick(View v) { System.out.println("unregisterReceiver"); MainActivity.this.unregisterReceiver(smsReceiver); } }
2.广播类
package com.example.mars_1800_broadcastreceiver02; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.gsm.SmsMessage; public class SMSReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { System.out.println("receive message"); //接收Intent对象当中的数据 Bundle bundle=intent.getExtras(); //在Bundle对象中有一个属性为pdus,该属性为Object数组 Object[] myOBJpdus=(Object[])bundle.get("pdus"); //创建一个SmsMessage类型的数组 SmsMessage[] messages=new SmsMessage[myOBJpdus.length]; System.out.println(messages.length); for (int i = 0; i < myOBJpdus.length; i++) { //使用Object数组中的对象创建SmsMessage对象 messages[i]=SmsMessage.createFromPdu((byte[])myOBJpdus[i]); //获取到信息对象的内容 System.out.println(messages[i].getDisplayOriginatingAddress()); System.out.println(messages[i].getDisplayMessageBody()); } } }
3.加入相应权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mars_1800_broadcastreceiver02" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mars_1800_broadcastreceiver02.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- <receiver android:name="SMSReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> --> </application> <!-- 接收短信广播则一定有加入相应的权限 --> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> </manifest>