ANDROID_MARS學習筆記_S01原始版_013_廣播機制二

來源:互聯網
上載者:User

標籤:

一、代碼
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 <Button14     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     <application12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             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     @Override18     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         @Override30         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         @Override46         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     @Override12     public void onReceive(Context context, Intent intent) {13         // TODO Auto-generated method stub14         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 block36             e.printStackTrace();37         }38     }39 40 }

 

ANDROID_MARS學習筆記_S01原始版_013_廣播機制二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.