我想挂断来电,检测到它,然后挂断。
问题是:这com.android.internal.telephony.ITelephony没有解决。
com.android.internal.telephony.ITelephony
我试图将包添加com.android.internal.telephony到我的应用程序并创建接口:
com.android.internal.telephony
package com.android.internal.telephony; public interface ITelephony { boolean endCall(); void answerRingingCall(); void silenceRinger(); }
但通话并未结束。
在这里,我检测到呼叫,显示toast(已显示),然后尝试挂断,但是正如我首先说的那样,com.android.internal.telephony.ITelephony在创建该程序包之前没有:
private class CallStateListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_RINGING: // called when someone is ringing to this phone Toast.makeText(ctx, "Incoming: " + incomingNumber, Toast.LENGTH_LONG).show(); try{ TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); Class c = Class.forName(tm.getClass().getName()); Method m = c.getDeclaredMethod("getITelephony"); com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm); telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm); telephonyService.silenceRinger(); telephonyService.endCall(); }catch (Exception e) { e.printStackTrace(); } break; } } }
我的清单和权限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".CallDetectService" android:enabled="true" android:exported="false" > </service> </application>
该ITelephony接口是内部接口,因此您无法获得对其的标准引用。您可以一直使用反射,即
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); Method m1 = tm.getClass().getDeclaredMethod("getITelephony"); m1.setAccessible(true); Object iTelephony = m1.invoke(tm); Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger"); Method m3 = iTelephony.getClass().getDeclaredMethod("endCall"); m2.invoke(iTelephony); m3.invoke(iTelephony);
但是,无论哪种方式,这些方法都需要MODIFY_PHONE_STATE权限,而该权限只能授予系统应用程序。因此,恐怕它还是不管用。