Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

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

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.ITelephony is not resolved.

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

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.ITelephony before I created that package:

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ITelephony interface is internal, so you cannot get a standard reference to it. You could use reflection all the way, i.e.

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_STATE permission, which can only be granted to system apps. So I'm afraid it won't work anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...