01_22_广播机制(二)
点击绑定,然后给号码123发送信息hello
-
//SMSReceiver.java
-
package mars.testbc2;
-
-
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{
-
-
/*
-
* 1、创建一个Bundle:bundle = intent.getExtras();
-
* Bundle用于Activity间通信,bundle类中加入数据(key -value的形式,
-
* 另一个activity里面取数据的时候,就要用到key,找出对应的value)
-
* 详细见:http://blog.csdn.net/randyjiawenjie/article/details/6651437
-
* 2、创建一个Object[]数组从Bundle中取数据:myoBJpdus = (OBject[])bundle.get("pdus");
-
* Object[]数组用法详见:http://blog.csdn.net/linbin130209114/article/details/6150143
-
* 3、创建一个大小为myOBJpdus.length的SmsMessage[]数组用于存放短信:
-
* messages = new SmsMessage[myOBJpdus.length];
-
* 4、从Object[]数组中得到短信:
-
* message = new SmsMessage.createFromPdu((byte[])myOBJpdus[i]);
-
* //使用pdu格式的短信数据生成短信对象
-
* 5、显示短信System.out.println(messages[i].getDisplayMessageBody());
-
* */
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
// TODO Auto-generated method stub
-
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]);
-
//调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容
-
System.out.println(messages[i].getDisplayMessageBody());
-
}
-
try {
-
// Thread.sleep(30 * 1000);
-
Thread.sleep(3);
-
System.out.println("-------------------------------");
-
} catch (InterruptedException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
}
-
//TestBC2Activity.java
-
package mars.testbc2;
-
-
import android.app.Activity;
-
import android.content.IntentFilter;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public class TestBC2Activity extends Activity {
-
/** Called when the activity is first created. */
-
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);
-
registerButton = (Button)findViewById(R.id.register);
-
registerButton.setOnClickListener(new RegisterReceiverListener());
-
unregisterButton = (Button)findViewById(R.id.unregister);
-
unregisterButton.setOnClickListener(new UnRegisterReceiverListener());
-
}
-
/*绑定监听器
-
* 1、创建一个BacastReciver对象:smsReceiver = new SMSReceiver()
-
* 2、创建一个IntentFilter对象:intentFilter = new IntentFilter()
-
* 3、为过滤器intentFilter添加Action:filter.addAction(SMS_ACTiON)
-
* 4、注册广播:TestBC2Activity.this.registerReceiver(smsReceiver,filter)
-
* 这样一个广播smsReceiver和filter就一一对应啦*/
-
class RegisterReceiverListener implements OnClickListener{
-
@Override
-
public void onClick(View v) {
-
System.out.println("click********RegisterReceiverListener");
-
//生成一个BroiadcastReceiver对象
-
smsReceiver = new SMSReceiver();
-
//生成一个IntentFilter对象
-
IntentFilter filter = new IntentFilter();
-
//为IntentFilter添加一个Action
-
filter.addAction(SMS_ACTION);
-
//将BroadcastReceiver对象注册到系统当中
-
TestBC2Activity.this.registerReceiver(smsReceiver, filter);
-
}
-
}
-
-
class UnRegisterReceiverListener implements OnClickListener{
-
@Override
-
public void onClick(View v) {
-
System.out.println("click********unRegisterReceiverListener");
-
//解除BroadcastReceiver对象的注册
-
TestBC2Activity.this.unregisterReceiver(smsReceiver);
-
}
-
}
-
}
也可以在AndroidMainfext.xml中注册receiver
-
<!--<receiver android:name="SMSReceiver">
-
<intent-filter>
-
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
-
</intent-filter>
-
</receiver>-->
结果:
04-10 23:12:44.312: I/System.out(330): click********RegisterReceiverListener
04-10 23:12:56.910: I/System.out(330): click********RegisterReceiverListener
04-10 23:13:29.990: I/System.out(330): receive message
04-10 23:13:30.000: I/System.out(330): 1
04-10 23:13:30.050: I/System.out(330): hello
04-10 23:13:30.120: I/System.out(330): -------------------------------
04-10 23:13:30.130: I/System.out(330): receive message
04-10 23:13:30.130: I/System.out(330): 1
04-10 23:13:30.141: I/System.out(330): hello
04-10 23:13:30.215: I/System.out(330): -------------------------------
04-10 23:14:02.320: I/System.out(330): click********unRegisterReceiverListener
阅读(682) | 评论(0) | 转发(0) |