Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> 輕松監聽Android接打電話

輕松監聽Android接打電話

編輯:Android開發實例

        我們要實現在Android中監聽接電話和打電話,其實很簡單,只需要兩個步驟,本文以實例的形式進行講解。

        1、第一步,寫一個Receiver繼承自BroadcastReceiver。

Java代碼
  1. public class PhoneStatReceiver extends BroadcastReceiver{   
  2.         private static final String TAG = "PhoneStatReceiver";    
  3. //        private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();   
  4.         private static boolean incomingFlag = false;  
  5.         private static String incoming_number = null;  
  6.  
  7.         @Override    
  8.         public void onReceive(Context context, Intent intent) {    
  9.                 //如果是撥打電話    
  10.                 if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                           
  11.                         incomingFlag = false;  
  12.                         String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);          
  13.                         Log.i(TAG, "call OUT:"+phoneNumber);                           
  14.                 }else{                           
  15.                         //如果是來電    
  16.                         TelephonyManager tm =    
  17.                             (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                         
  18.  
  19.                         switch (tm.getCallState()) {    
  20.                         case TelephonyManager.CALL_STATE_RINGING:    
  21.                                 incomingFlag = true;//標識當前是來電    
  22.                                 incoming_number = intent.getStringExtra("incoming_number");    
  23.                                 Log.i(TAG, "RINGING :"+ incoming_number);    
  24.                                 break;    
  25.                         case TelephonyManager.CALL_STATE_OFFHOOK:                                   
  26.                                 if(incomingFlag){    
  27.                                         Log.i(TAG, "incoming ACCEPT :"+ incoming_number);    
  28.                                 }    
  29.                                 break;    
  30.                         case TelephonyManager.CALL_STATE_IDLE:                                   
  31.                                 if(incomingFlag){    
  32.                                         Log.i(TAG, "incoming IDLE");                                   
  33.                                 }    
  34.                                 break;    
  35.                         }    
  36.                 }    
  37.         }    
  38. }  

       第二步:在AndroidManifest.xml,配置寫好的Receiver,並攔截相應的BroadCastAction, 另外注意加上相應的權限。

XML/HTML代碼
  1. <receiver android:name=".filter.PhoneStatReceiver">    
  2.             <intent-filter>  
  3.                  <action android:name="android.intent.action.PHONE_STATE"/>             
  4.                  <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  
  5.             </intent-filter>  
  6. </receiver>  
  7.   
  8. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
  9. <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved