我正在尝试使用此方法阻止Android中的来电
我尝试了很多方法,但未能中止传入呼叫,请帮助解决此代码的问题。我还在com.android.internal.telephony.ITelephony中添加了ITelephony接口。
例外
码
String Messageofenemy;
String Blckno;
public OutgoingReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
try {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MyService.class);
context.startService(i);
return;
}
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
//outgoing call code here
} else {
//get the phone state
String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ?
intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null;
Bundle bundle = intent.getExtras();
if (newPhoneState != null &&
newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//read the incoming call number
String phoneNumber =
bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
db = new DatabaseHelper(context);
Cursor res = db.getAllrows();
if (res.getCount() == 0) {
} else {
while (res.moveToNext()) {
Blckno = res.getString(0);
}
}
if (phoneNumber.equals("03352264769")) {
try {
AudioManager am =
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
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);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
} catch (Exception e) {
Toast.makeText(ctx, "Exception" + String.valueOf(e),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(context, "Exception is " + String.valueOf(e),
Toast.LENGTH_LONG).show();
}
}
}
}
} catch (Exception ex) {
Toast.makeText(context, "Error is " + String.valueOf(ex),
Toast.LENGTH_LONG).show();
}
}
}
最佳答案
检查呼叫状态和onReceive()
上的来电号码。然后使用disconnectPhoneItelephony(Context context)
中止来电-
public class OutgoingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
if (!intent.getAction().equals("android.intent.action.PHONE_STATE") || intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
Toast.makeText(context, "Not phone state", Toast.LENGTH_LONG).show();
//return;
// Else, try to do some action
} else {
if (number == null) { //get the incomming call number
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
disconnectPhoneItelephony(context); // this is method whick blocks incoming call, this method is provided on the down
}
}
}
}
// Method to disconnect phone automatically and programmatically
// Keep this method as it is
@SuppressWarnings({ "rawtypes", "unchecked" })
private void disconnectPhoneItelephony(Context context) {
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 disconect call....");
}
}
}
这是接口
ITelephony.java
-interface ITelephony {
boolean endCall();
}
并且不要忘记在
BroadcastReceiver
文件的manifest.xml
类上添加权限和意图过滤器- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver
android:name=".OutgoingReceiver"
android:enabled="true">
<intent-filter android:priority="9999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL"
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
</intent-filter>
</receiver>