Android] 使用廣播接收器Braodcast Receiver 監聽手機來電 去電 掛斷的事

Braodcast Receiver顧名思義就是廣播接收器,它和時間處理機制相似,可是事件處理機制是程序組件級別的(好比:按鈕的單擊事件),而廣播事件處理機制是系統級別的。咱們能夠用Intent來啓動一個組件,也能夠用sendBroadcast()方法發起一個系統級別的事件廣播來傳遞消息。咱們一樣能夠在本身的應用程序中實現Broadcast Receiver來監聽和響應廣播的Intent。java

事件的廣播經過建立Intent對象並調用sendBroadcast()方法將廣播發出。事件的接受是經過定義一個繼承BroadcastReceiver的類來實現的,繼承該類後覆蓋其onReceive()方法,在該方法中響應事件。android

public class PhoneReceiver extends BroadcastReceiver {

 
@Override
public void onReceive(Context context, Intent intent){
// TODO Auto-generated method stub

}

}



下面咱們就用廣播接收器實現電話狀態改變時啓動(來電、接聽、掛斷、撥打電話),可是暫時無法實現撥打電話時判斷對方是否接聽、轉語音信箱等。Android在電話狀態改變是會發送action爲android.intent.action.PHONE_STATE的廣播,而撥打電話時會發送action爲android.intent.action.NEW_OUTGOING_CALL的廣播,可是我看了下開發文檔,暫時沒發現有來電時的廣播。知道這個就好辦了,咱們寫個BroadcastReceiver用於接收這兩個廣播就能夠了。ide

public class PhoneReceiver extends BroadcastReceiver {

         
        @Override
        public void onReceive(Context context, Intent intent){
                // TODO Auto-generated method stub
                System.out.println("action"+intent.getAction());
                if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
                        //若是是去電(撥出)
                        System.out.println("撥出");
                }else{
                        //查了下android文檔,貌似沒有專門用於接收來電的action,因此,非去電即來電
                        System.out.println("來電");
                        TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
                        tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
                        //設置一個監聽器
                }
        }
        PhoneStateListener listener=new PhoneStateListener(){
 
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                        // TODO Auto-generated method stub
                        //state 當前狀態 incomingNumber,貌似沒有去電的API
                        super.onCallStateChanged(state, incomingNumber);
                        switch(state){
                        case TelephonyManager.CALL_STATE_IDLE:
                                System.out.println("掛斷");
                                break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
                                System.out.println("接聽");
                                break;
                        case TelephonyManager.CALL_STATE_RINGING:
                                System.out.println("響鈴:來電號碼"+incomingNumber);
                                //輸出來電號碼
                                break;
                        }
                } 
        };
}


由於咱們這是監聽的系統事件,因此須要AndroidManifest.xml註冊廣播接收器:spa

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


不要忘記添加權限code

    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
相關文章
相關標籤/搜索