Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> [android] 手機衛士接收打電話廣播顯示號碼歸屬地,android衛士

[android] 手機衛士接收打電話廣播顯示號碼歸屬地,android衛士

編輯:關於android開發

[android] 手機衛士接收打電話廣播顯示號碼歸屬地,android衛士


使用廣播接收者接收打電話的意圖,顯示號碼歸屬地

 

新建一個類OutCallReceiver繼承系統的BroadcastReceiver

重寫onReceive()方法

調用getResultData()方法,獲取到String電話號碼,查詢數據庫獲取到歸屬地

package com.qingguow.mobilesafe.receiver;

import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
 * 接收打電話廣播
 * @author taoshihan
 *
 */
public class OutcallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        String phone=getResultData();
        String address=NumberQueryAddressUtil.queryAddress(phone);
        System.out.println("111"+address);
        Toast.makeText(arg0, address, 1).show();
    }

}

 

 

清單文件中定義<receiver>節點,和<intent-filter>節點,設置<action>節點名稱,android:name=”android.intent.action.NEW_OUTGOING_CALL”

需要權限,android.permission.PROCESS_OUTGOING_CALLS

 代碼注冊廣播

此時的廣播接收者,只要一打電話就會被調用,用戶體驗不好,我們使用代碼注冊廣播接收者,用設置中心的開啟監聽電話來電的部分來控制這個廣播

 

在服務裡面調用registerReceiver()方法,注冊廣播,參數:BroadcastReceiver對象,IntentFilter對象

獲取IntentFilter對象,調用IntentFilter對象的addAction()方法,參數:android.intent.action.NEW_OUTGOING_CALL

 

在服務裡面解除注冊廣播,調用unregisterReceiver()方法,參數:BroadcastReceiver對象

 

package com.qingguow.mobilesafe.service;

import com.qingguow.mobilesafe.receiver.OutcallReceiver;
import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

/**
 * 來電顯示
 * 
 * @author taoshihan
 * 
 */
public class AddressService extends Service {
    private TelephonyManager tm;
    private MyPhoneStateListener phoneStateListener;
    private OutcallReceiver outcallReceiver;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    /**
     * 服務創建
     */
    @Override
    public void onCreate() {
        super.onCreate();
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        phoneStateListener = new MyPhoneStateListener();
        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        
        //注冊廣播
        outcallReceiver=new OutcallReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        registerReceiver(outcallReceiver, filter);
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                String info = NumberQueryAddressUtil
                        .queryAddress(incomingNumber);
                Toast.makeText(getApplicationContext(), info, 1).show();
                break;
            default:
                break;
            }
        }
    }
    /**
     * 服務銷毀
     */
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        //取消監聽
        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
        phoneStateListener=null;
        //解除注冊廣播
        unregisterReceiver(outcallReceiver);
        outcallReceiver=null;
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved