ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一、代码
1.xml
(1)main.xml

 <?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/register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定监听器"
/>
<Button
android:id="@+id/unregister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解除监听器绑定"
/> </LinearLayout>

(2)AndroidManifest.xml.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast2"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TestBC2Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>

2.java
(1)TestBC2Activity.java

 package com.broadcast2;

 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());
} class RegisterReceiverListener implements OnClickListener{ @Override
public void onClick(View v) {
//生成一个BroiadcastReceiver对象
smsReceiver = new SMSReceiver();
//生成一个IntentFilter对象
IntentFilter filter = new IntentFilter();
//为IntentFilter添加一个Action,决定了reciver能接收什么类型的请求
filter.addAction(SMS_ACTION);
//将BroadcastReceiver对象注册到系统当中
TestBC2Activity.this.registerReceiver(smsReceiver, filter);
} } class UnRegisterReceiverListener implements OnClickListener{ @Override
public void onClick(View v) {
//解除BroadcastReceiver对象的注册
TestBC2Activity.this.unregisterReceiver(smsReceiver);
} }
}

(2)SMSReceiver.java

 package com.broadcast2;

 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{ @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);
System.out.println("-------------------------------");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
上一篇:python第二十课——math模块中常用的函数


下一篇:Python 之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy