1. 程式人生 > >黑名單電話自動攔截【Android】

黑名單電話自動攔截【Android】

1.功能描述: 

當前手機中儲存了一些黑名單電話號(如110等)

當一個電話打入進來, 如果它剛好是一個黑名單號碼, 就會自動將電話結束通話

過程分析:

啟動服務

在服務中監聽電話狀態, 

當電話狀態是響鈴時, 判斷是否為黑名單號

如果是, 結束通話電話

2.相關API

TelephonyManager: 電話服務的管理器

       context.getSystemService(Context.TELEPHONY_SERVICE)

: 得到它的物件

       listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE)

:監聽電話狀態: 



PhoneStateListener : 電話狀態監聽器

      onCallStateChanged(int state, String incomingNumber)

: 電話狀態改變的回撥方法

      TelephonyManager.CALL_STATE_IDLE : 空閒狀態

      TelephonyManager.CALL_STATE_RINGING : 響鈴狀態

      TelephonyManager.CALL_STATE_OFFHOOK : 接通狀態

3.結束通話電話

說明:Android沒有對外公開結束通話的API,如果需要結束通話,必須使用AIDL與電話管理服務進行通訊,並呼叫服務中的API實現結束通話,方法如下:


1).從sdk的原始碼中複製下面一個檔案
 com/android/internal/telephony/ITelephony.aidl


2).呼叫ITelephony.endCall()結束通話
 Method method = Class.forName("android.os.ServiceManager")
  .getMethod("getService", String.class);
 IBinder binder = (IBinder)method.invoke(null, Context.TELEPHONY_SERVICE);
 ITelephony telephony = ITelephony.Stub.asInterface(br);
 telephony.endCall(); inde


3). 宣告打/結束通話電話的許可權
 <uses-permission android:name="android.permission.CALL_PHONE" />

4.程式碼實現

1).ListenCallService

package com.example.appservice;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class ListenCallService extends Service {

	private TelephonyManager tm;
	private PhoneStateListener listener = new PhoneStateListener() {

		// 當通話狀態發生改變時呼叫
		public void onCallStateChanged(int state, String incomingNumber) {
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:// 空閒(結束通話電話/未來電之前)
				Log.e("TAG", "空閒(結束通話電話/未來電之前)");
				break;

			case TelephonyManager.CALL_STATE_RINGING:// 響鈴
				Log.e("TAG", "響鈴");
				// 如果來電是黑名單號(110),就結束通話電話
				if ("110".equals(incomingNumber)) {
					try {
						endCall();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;

			case TelephonyManager.CALL_STATE_OFFHOOK:// 接通
				Log.e("TAG", "接通");
				break;
			default:
				break;
			}
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * 結束通話電話
	 */
	private void endCall() throws Exception {
		// 通過反射呼叫隱藏的API
		// 得到隱藏類的Class物件
		Class c = Class.forName("android.os.ServiceManager");
		// 得到方法所對應的Method物件
		Method method = c.getMethod("getService", String.class);
		// 呼叫方法
		IBinder iBinder = (IBinder) method.invoke(null,
				Context.TELEPHONY_SERVICE);
		// 得到介面物件
		ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
		// 結束通話
		telephony.endCall();

	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		// 得到電話管理器
		tm = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		// 監聽電話狀態
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 停止電話監聽
		tm.listen(listener, PhoneStateListener.LISTEN_NONE);
	}
}
2).黑名單攔截功能的開關
<span style="white-space:pre">	</span>public void startListenCall(View v){
		startService(new Intent(this, ListenCallService.class));
	}
	
	public void stopListenCall(View v){
		stopService(new Intent(this, ListenCallService.class));
	}

3).註冊Service

<service android:name="com.example.appservice.ListenCallService"></service>

5.設定開機即開啟黑名單

<receiver android:name="com.example.appservice.BootReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
package com.example.appservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
 * 接收開機完成廣播的receiver
 * @author Xiaocici
 *
 */
public class BootReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		//啟動電話監聽的receiver
		context.startService(new Intent(context, ListenCallService.class));

	}

}