我将使用ITelephony.aidl的endCall方法以编程方式断开所有来电。
这是我的BroadcastReceiver:
public class CallBlocker extends BroadcastReceiver {
String number;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals("android.intent.action.PHONE_STATE")) {
disconnectIncomingCall(context);
}
}
private void disconnectIncomingCall(Context context) {
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
IBinder binder = (IBinder) m.invoke(null, new Object[]{TELEPHONY_SERVICE});
telephonyService = ITelephony.Stub.asInterface(binder);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的服务:
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ITelephony.Stub() {
@Override
public boolean endCall() throws RemoteException {
return false;
}
@Override
public void answerRingingCall() throws RemoteException {
}
@Override
public void silenceRinger() throws RemoteException {
}
};
}
}
这是ITelephony.aidl文件:
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
我在Manifest中声明了Service和BroadcastReceiver。
当我运行项目并且我连接呼叫时,我收到此错误:
java.lang.NullPointerException: null receiver
在这一行:
IBinder binder = (IBinder) m.invoke(null, new Object[]{TELEPHONY_SERVICE});
如何在android中以编程方式阻止所有来电?