如何将com.android.internal.telephony.ITelephony导入Android应用程序

时间:2014-07-04 20:37:10

标签: java android phone-call

我想挂断来电我检测到它然后我想把它挂起来。

问题在于:com.android.internal.telephony.ITelephony未解决。

我尝试将包com.android.internal.telephony添加到我的应用程序并创建界面:

package com.android.internal.telephony;

public interface ITelephony {      

    boolean endCall();     

    void answerRingingCall();      

    void silenceRinger(); 

}

但是电话没有结束。

在这里我检测到呼叫,显示吐司(显示)然后尝试挂断,但正如我先说的那样,在我创建该包之前没有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>



</manifest>

2 个答案:

答案 0 :(得分:16)

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权限,这只能授予系统应用程序。所以我担心它无论如何都不会工作。

答案 1 :(得分:1)

对于Kotlin:

 val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
 val m1 = tm::class.java.getDeclaredMethod("getITelephony")
 m1.setAccessible(true)
 val telephonyService = m1.invoke(tm)
 val m2 = telephonyService::class.java.getDeclaredMethod("silenceRinger")
 val m3 = telephonyService::class.java.getDeclaredMethod("endCall")

 m2.invoke(telephonyService)
 m3.invoke(telephonyService)