ANDROID_MARS学习笔记_S01原始版_013_广播机制二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ANDROID_MARS学习笔记_S01原始版_013_广播机制二相关的知识,希望对你有一定的参考价值。
一、代码
1.xml
(1)main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <Button 8 android:id="@+id/register" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="绑定监听器" 12 /> 13 <Button 14 android:id="@+id/unregister" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="解除监听器绑定" 18 /> 19 20 </LinearLayout>
(2)AndroidManifest.xml.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.broadcast2" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".TestBC2Activity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 <uses-permission android:name="android.permission.RECEIVE_SMS"/> 27 </manifest>
2.java
(1)TestBC2Activity.java
1 package com.broadcast2; 2 3 import android.app.Activity; 4 import android.content.IntentFilter; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class TestBC2Activity extends Activity { 11 /** Called when the activity is first created. */ 12 private Button registerButton = null; 13 private Button unregisterButton = null; 14 private SMSReceiver smsReceiver = null; 15 16 private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED"; 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 registerButton = (Button)findViewById(R.id.register); 22 registerButton.setOnClickListener(new RegisterReceiverListener()); 23 unregisterButton = (Button)findViewById(R.id.unregister); 24 unregisterButton.setOnClickListener(new UnRegisterReceiverListener()); 25 } 26 27 class RegisterReceiverListener implements OnClickListener{ 28 29 @Override 30 public void onClick(View v) { 31 //生成一个BroiadcastReceiver对象 32 smsReceiver = new SMSReceiver(); 33 //生成一个IntentFilter对象 34 IntentFilter filter = new IntentFilter(); 35 //为IntentFilter添加一个Action,决定了reciver能接收什么类型的请求 36 filter.addAction(SMS_ACTION); 37 //将BroadcastReceiver对象注册到系统当中 38 TestBC2Activity.this.registerReceiver(smsReceiver, filter); 39 } 40 41 } 42 43 class UnRegisterReceiverListener implements OnClickListener{ 44 45 @Override 46 public void onClick(View v) { 47 //解除BroadcastReceiver对象的注册 48 TestBC2Activity.this.unregisterReceiver(smsReceiver); 49 } 50 51 } 52 }
(2)SMSReceiver.java
1 package com.broadcast2; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.telephony.SmsMessage; 8 9 public class SMSReceiver extends BroadcastReceiver{ 10 11 @Override 12 public void onReceive(Context context, Intent intent) { 13 // TODO Auto-generated method stub 14 System.out.println("receive message"); 15 16 //此例子是获取短信内容 17 //接受Intent对象当中的数据 18 Bundle bundle = intent.getExtras(); 19 //在Bundle对象当中有一个属性名为pdus,这个属性的值是一个Object数组 20 Object[] myOBJpdus = (Object[]) bundle.get("pdus"); 21 //创建一个SmsMessage类型的数组 22 SmsMessage[] messages = new SmsMessage[myOBJpdus.length]; 23 System.out.println(messages.length); 24 for (int i = 0; i<myOBJpdus.length; i++) 25 { 26 //使用Object数组当中的对象创建SmsMessage对象 27 messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]); 28 //调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容 29 System.out.println(messages[i].getDisplayMessageBody()); 30 } 31 try { 32 Thread.sleep(30 * 1000); 33 System.out.println("-------------------------------"); 34 } catch (InterruptedException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 }
以上是关于ANDROID_MARS学习笔记_S01原始版_013_广播机制二的主要内容,如果未能解决你的问题,请参考以下文章
ANDROID_MARS学习笔记_S01原始版_005_RadioGroupCheckBoxToast
ANDROID_MARS学习笔记_S01原始版_009_下载文件
ANDROID_MARS学习笔记_S01原始版_014_WIFI
ANDROID_MARS学习笔记_S01原始版_016_Service