在Android實現廣播接收

來源:互聯網
上載者:User

Android使用過程中,如果你想竊聽別人接收到的簡訊,達到你不可告人的目的,那麼本節內容可以實現你的需求。當系統收到簡訊時,會發出一個action名稱為android.provider.Telephony.SMS_RECEIVED的廣播Intent,該Intent存放了接收到的簡訊內容,使用名稱“pdus”即可從Intent中擷取簡訊內容。這裡面得到對象數組,數組是以位元組格式

 
  1. public class SmsBroadcastReceiver extends BroadcastReceiver {  
  2.         @Override  
  3.         public void onReceive(Context context, Intent intent) {  
  4.                 Object[] pduses=(Object[])intent.getExtras().get("pdus");  
  5.                 for(Object pdus: pduses){  
  6.                         byte[] pdusSms=(byte[])pdus;  
  7.                         SmsMessage smsMessage=SmsMessage.createFromPdu(pdusSms);  
  8.                         String mobile=smsMessage.getOriginatingAddress();//獲得發簡訊手機  
  9.                         String content=smsMessage.getMessageBody();//獲得簡訊內容  
  10.                         Date date= new Date(smsMessage.getTimestampMillis());//獲得簡訊發送時間  
  11.                         SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  12.                         String sendDate=simpleDateFormat.format(date);  
  13.                 }  
  14.         }  
  15. }  
  16. 在AndroidManifest.xml檔案中的<application>節點裡對接收到簡訊的廣播Intent進行訂閱:  
  17. <receiver android:name=".IncomingSMSReceiver"> 
  18. <intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver> 
  19. 在AndroidManifest.xml檔案中添加以下許可權:  
  20. <uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收簡訊許可權 --> 
  21. <uses-permission android:name="android.permission.SEND_SMS"/><!-- 傳送簡訊許可權 --> 

廣播接收者

除了簡訊到來廣播Intent,Android還有很多廣播Intent,如:開機啟動、電池電量變化、時間已經改變等廣播Intent。
 接收電池電量變化廣播Intent ,在AndroidManifest.xml檔案中的<application>節點裡訂閱此Intent:。

 
  1. <receiver android:name=".IncomingSMSReceiver"> 
  2.     <intent-filter> 
  3.          <action android:name="android.intent.action.BATTERY_CHANGED"/> 
  4.     </intent-filter> 
  5. </receiver> 
  6.  接收開機啟動廣播Intent,在AndroidManifest.xml檔案中的<application>節點裡訂閱此Intent:  
  7. <receiver android:name=".IncomingSMSReceiver"> 
  8.     <intent-filter> 
  9.          <action android:name="android.intent.action.BOOT_COMPLETED"/> 
  10.     </intent-filter> 
  11. </receiver> 

並且要進行許可權聲明:

 
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

Android開發執行個體詳解之IMF

Android使用者介面設計:線性布局

Android使用者介面設計:布局基礎

GoogleAndroid UI設計技巧:新的UI設計模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.