我正在尝试使用 Android Telephony Manager 结束通话(新的 Call Screening API 不适合我)。我在网上找到了很多例子,都和我下面的代码非常相似。它编译得很好,但我在线上得到了一个运行时 ClassCastException
telephonyService = (ITelephony) m.invoke(tm);
例外是
java.lang.ClassCastException:
com.android.internal.telephony.ITelephony$Stub$Proxy
cannot be cast to com.ramrod.MyApp.IncomingCallReceiver$ITelephony
我的代码是
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ITelephony telephonyService;
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method m = tm.getClass().getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
if ((number != null)) {
telephonyService.endCall();
}
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(context, "Ring " + number, Toast.LENGTH_SHORT).show();
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Toast.makeText(context, "Answered " + number, Toast.LENGTH_SHORT).show();
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context, "Idle "+ number, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
我也试过导入com.android.internal.telephony(包括 ITelephony),但这不符合要求。
互换的青春
相关分类