1. 程式人生 > >android監聽來電去電廣播

android監聽來電去電廣播

要監聽android打電話和接電話,只需下面2步驟
1.第一步,寫一個Receiver繼承自BroadcastReceiver

class MyPhoneStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch(state) {
            case TelephonyManager.CALL_STATE_IDLE: //空閒
                break;
            case TelephonyManager.CALL_STATE_RINGING: //來電
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK: //摘機(正在通話中)
                break;
            }
        }
        
    }


         //獲取電話通訊服務
        TelephonyManager tpm = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        //建立一個監聽物件,監聽電話狀態改變事件
        tpm.listen(new MyPhoneStateListener(),
                PhoneStateListener.LISTEN_CALL_STATE);
許可權:

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

要監聽android打電話和接電話,只需下面2步驟
第一步,寫一個Receiver繼承自BroadcastReceiver

來電action為TelephonyManager.ACTION_PHONE_STATE_CHANGED

去電action為Intent.ACTION_NEW_OUTGOING_CALL

public class PhoneStatReceiver extends BroadcastReceiver{
        
        private static final String TAG = "PhoneStatReceiver";
        
//        private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        
        private static boolean incomingFlag = false;
        
        private static String incoming_number = null;

        @Override
        public void onReceive(Context context, Intent intent) {
                //如果是撥打電話
                if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                        
                        incomingFlag = false;
                        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);        
                        Log.i(TAG, "call OUT:"+phoneNumber);                        
                }else{                        
                        //如果是來電
                        TelephonyManager tm = 
                            (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                        
                        
                        switch (tm.getCallState()) {
                        case TelephonyManager.CALL_STATE_RINGING:
                                incomingFlag = true;//標識當前是來電
                                incoming_number = intent.getStringExtra("incoming_number");
                                Log.i(TAG, "RINGING :"+ incoming_number);
                                break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:                                
                                if(incomingFlag){
                                        Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
                                }
                                break;
                        
                        case TelephonyManager.CALL_STATE_IDLE:                                
                                if(incomingFlag){
                                        Log.i(TAG, "incoming IDLE");                                
                                }
                                break;
                        } 
                }
        }
}

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

<receiver android:name=".filter.PhoneStatReceiver">  
            <intent-filter>
                 <action android:name="android.intent.action.PHONE_STATE"/>           
                 <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
</receiver>

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>