編輯:關於Android編程
本文實例為大家分享了Android來電攔截的方法,供大家參考,具體內容如下
權限
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <!-- 注冊廣播 監聽撥出電話 --> <receiver android:name="com.example.administrator.endcall.PhoneStateReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver>
撥號廣播—PhoneStateReceiver
package com.example.administrator.endcall; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; public class PhoneStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); Log.i("BlockCallHelper", "BlockCallHelper------->>>>" + phoneNumber); } } }
來電掛斷
BlockCallHelper
package com.example.administrator.endcall; import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.RemoteException; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import com.android.internal.telephony.ITelephony; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public final class BlockCallHelper { private static final String TAG = "BlockCallHelper"; private Context mContext; private TelephonyManager tManger; private List<String> phones; private BlockCallBack bcb; ////////////////////////////////////////// private static final class Factory { private static final BlockCallHelper instance = new BlockCallHelper(); } public static BlockCallHelper getInstance() { return Factory.instance; } /** * 初始化上下文以及數據 * @param context */ public BlockCallHelper init(Context context) { if (context == null) { throw new NullPointerException("context == null!"); } this.mContext = context; this.tManger = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); tManger.listen(new PhoneCallListener(), PhoneCallListener.LISTEN_CALL_STATE); return this; } /** * 注入需要攔截的手機號 * @param phoneL */ public BlockCallHelper injectBlockPhoneNum(ArrayList<String> blockCalls) { this.phones = blockCalls; return this; } /** * 結束通話 */ private void endCall() { Class<TelephonyManager> tmc = TelephonyManager.class; Method getITelephonyMethod; try { getITelephonyMethod = tmc.getDeclaredMethod("getITelephony", (Class[]) null); getITelephonyMethod.setAccessible(true); ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(tManger, (Object[]) null); iTelephony.endCall(); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (InvocationTargetException e2) { e2.printStackTrace(); } catch (IllegalAccessException e3) { e3.printStackTrace(); } catch (RemoteException e4) { e4.printStackTrace(); } } private final class PhoneCallListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { if (state == TelephonyManager.CALL_STATE_RINGING) { if (phones.contains(incomingNumber)) { Log.i("BlockCallHelper", "contains contains contains"); endCall(); if (bcb != null) { bcb.callBack(incomingNumber); } } else { Log.i("BlockCallHelper", "contains not-------"); } } } } public BlockCallHelper setBlockCallBack(BlockCallBack back) { this.bcb = back; return this; } public interface BlockCallBack { void callBack(String incomingNum); } }
看主界面MainActivity
package com.example.administrator.endcall; import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<String> blockCalls = new ArrayList(); blockCalls.add("13581922339"); blockCalls.add("+8613581922339"); blockCalls.add("18500813370"); blockCalls.add("13717717622"); blockCalls.add("+8613717717622"); BlockCallHelper.getInstance().init(this).injectBlockPhoneNum(blockCalls).setBlockCallBack(new BlockCallHelper.BlockCallBack() { @Override public void callBack(String incomingNum) { Log.i("BlockCallHelper", "incomingNum-----------" + incomingNum); } }); } }
最後看AIdl層面
ITelephony.aidl
package com.android.internal.telephony; interface ITelephony { void dial(String number); void call(String number); boolean showCallScreen(); boolean showCallScreenWithDialpad(boolean showDialpad); boolean endCall(); void answerRingingCall(); void silenceRinger(); boolean isOffhook(); boolean isRinging(); boolean isIdle(); boolean isRadioOn(); boolean isSimPinEnabled(); void cancelMissedCallsNotification(); boolean supplyPin(String pin); boolean handlePinMmi(String dialString); void toggleRadioOnOff(); boolean setRadio(boolean turnOn); void updateServiceLocation(); void enableLocationUpdates(); void disableLocationUpdates(); int enableApnType(String type); int disableApnType(String type); boolean enableDataConnectivity(); boolean disableDataConnectivity(); boolean isDataConnectivityPossible(); Bundle getCellLocation(); List<NeighboringCellInfo> getNeighboringCellInfo(); int getCallState(); int getDataActivity(); int getDataState(); }
NeighboringCellInfo.aidl
// NeighboringCellInfo.aidl package android.telephony; parcelable NeighboringCellInfo;
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
當我們在移動端閱讀文章時,總會有朋友發了一條微信給你。這時候,我是繼續看文章還是回微信呢?MIUI8的微信多窗口功能讓你徹底擺脫這個煩惱,多窗口模式能夠讓你
前言:在進入今天正題前,還是老樣子先談談感想吧,最近感覺整個都失去了方向感,好迷茫!找工作又失敗了,難道Android真的飽和了?這兩天我一直沒出門,除了下樓哪外賣就是宅
當我們需要執行一些耗時操作,比如說發起一條網絡請求時,考慮到網速等其他原因,服務器未必會立刻響應我們的請求,如果不將這類操作放在子線程裡去運行,就會導致主線程被阻塞住,從
一、JNI概述 JNI 是Java Native Interface的縮寫,中文翻譯為“Java本地調用”,JNI 是本地編程接口。它使得在 Java 虛擬機 (VM)