我想拒绝 android 中的传入,我从这些链接中看到了很多代码。
但是我还是做不到,谁能告诉我简单的步骤怎么做?
我想拒绝 android 中的传入,我从这些链接中看到了很多代码。
但是我还是做不到,谁能告诉我简单的步骤怎么做?
为了拦截您的电话,您只需:
1. 制作一个名为的包。com.android.internal.telephony;
2. 在这个包中创建一个名为 ITelephony 的接口文件。
并将此代码写入该接口文件中。
boolean endCall();
void answerRingingCall();
void silenceRinger();
现在在您要拦截调用的类中将该类扩展为BroadcastReceiver
并在onReceive()
函数中编写以下代码。
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.d("INCOMING", phoneNumber);
if ((phoneNumber != null)) {
telephonyService.endCall();
Log.d("HANG UP", phoneNumber);
}
} catch (Exception e) {
e.printStackTrace();
}
而已。
提供适当的权限并添加ITelephony.java
文件
private void declinePhone(Context context) throws Exception {
try {
String serviceManagerName = "android.os.ServiceManager";
String serviceManagerNativeName = "android.os.ServiceManagerNative";
String telephonyName = "com.android.internal.telephony.ITelephony";
Class<?> telephonyClass;
Class<?> telephonyStubClass;
Class<?> serviceManagerClass;
Class<?> serviceManagerNativeClass;
Method telephonyEndCall;
Object telephonyObject;
Object serviceManagerObject;
telephonyClass = Class.forName(telephonyName);
telephonyStubClass = telephonyClass.getClasses()[0];
serviceManagerClass = Class.forName(serviceManagerName);
serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
Method getService = // getDefaults[29];
serviceManagerClass.getMethod("getService", String.class);
Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
Binder tmpBinder = new Binder();
tmpBinder.attachInterface(null, "fake");
serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
telephonyObject = serviceMethod.invoke(null, retbinder);
telephonyEndCall = telephonyClass.getMethod("endCall");
telephonyEndCall.invoke(telephonyObject);
} catch (Exception e) {
e.printStackTrace();
Log.d("unable", "msg cant dissconect call....");
}
您将需要一个带有意图过滤器的广播接收器,我认为 ACTION_ANSWER
更多细节在这里:http: //developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER
您可以使用清单文件或方法 registerReceiver 注册它
更多信息在这里: http: //developer.android.com/reference/android/content/Context.html#registerReceiver (android.content.BroadcastReceiver, android.content.IntentFilter)
您还需要在清单文件中指定适当的权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
您提供的链接非常好。你只需要更多的耐心:)
快乐编码。
这是阻止所有来电的最佳教程,您可以对其进行自定义以阻止仅选定号码的来电。
在其中创建一个接口com.android.internal.telephony
并命名为
interface ITelephony {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
boolean endCall();
void silenceRinger();strong text
void answerRingingCall();
}
// then catch the incoming call intent and call below method
if (intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) != null) {
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "INCOMING call catched: ", Toast.LENGTH_SHORT).show();
disconnectPhoneItelephony(mContext)
}
private void disconnectPhoneItelephony(Context context) {
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
SessionManager.getInstance(context).setBlockStatusAllow("BLOCKED");
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
在清单中添加必要的权限
如果您要构建一个自助服务终端应用程序,那么我建议您获取通知设置并将设备设置为“请勿打扰”模式。
var mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)