本文实例为大家分享了Android来电拦截的方法,供大家参考,具体内容如下 权限 <uses-permission android:name="android.pe
本文实例为大家分享了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;
}
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;
}
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;
您可能感兴趣的文章:Android加载Gif动画实现代码Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现android-wheel控件实现三级联动效果Android自定义UI手势密码改进版源码下载Android自定义UI手势密码改进版Android指纹解锁方法解析Android编程实现tcp、UDP客户端通信功能示例Android中button点击后字体的变色效果Android中使用TextView实现高仿京东淘宝各种倒计时效果Android 屏幕横竖切换详解
--结束END--
本文标题: Android来电拦截的实现方法
本文链接: https://lsjlt.com/news/23448.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0