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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24580508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:11:17  来源:igfitidea点击:

How to import com.android.internal.telephony.ITelephony to the Android application

javaandroidphone-call

提问by Yoda

I want to hang up incoming call I detect it and then I want to hang it up.

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

The problem is that this: com.android.internal.telephony.ITelephonyis not resolved.

问题是这个:com.android.internal.telephony.ITelephony没有解决。

I tried to adding package com.android.internal.telephonyto my application and create interface:

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

package com.android.internal.telephony;

public interface ITelephony {      

    boolean endCall();     

    void answerRingingCall();      

    void silenceRinger(); 

}

but the call is not ended.

但通话并没有结束。

Here I detect call, display toast(it is displayed) then try to hang up but as I said first there was no com.android.internal.telephony.ITelephonybefore I created that package:

在这里,我检测到呼叫,显示吐司(显示)然后尝试挂断,但正如我首先所说的那样,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;
            }
        }
    }

My Manifest and permissions:

我的清单和权限:

<?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>

采纳答案by matiash

The ITelephonyinterface is internal, so you cannot get a standard reference to it. You coulduse reflection all the way, i.e.

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);

But either way those methods need the MODIFY_PHONE_STATEpermission, which can only be granted to system apps. So I'm afraid it won't work anyway.

但无论哪种方式,这些方法都需要MODIFY_PHONE_STATE权限,该权限只能授予系统应用程序。所以恐怕无论如何都行不通。

回答by Hymans Nogueira

For 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)