Question

I am using Itelephony service to end the call programmatically. It’s working on HTC android version 4.1 but not working on SAMSUNG GT-S7562 android version 4.0.4 Here is my code

TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            System.out.println("telephonyManager..."+telephonyManager);
            Class clazz = null;
            try {
                clazz = Class.forName(telephonyManager.getClass().getName());
                System.out.println("clazz..."+clazz);
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Method method = null;
            try 
            {
                method = clazz.getDeclaredMethod("getITelephony");
                System.out.println("method..."+method);
            } catch (NoSuchMethodException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            method.setAccessible(true);
            ITelephony telephonyService = null;
            try 
            {
                telephonyService = (ITelephony) method.invoke(telephonyManager);
            } catch (IllegalArgumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try
            {
                telephonyService.endCall();
                Intent   intent = new Intent(OutGoingScreen.this, Dialpad.class);
                startActivity(intent); 
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

My log at is showing this

10-09 09:26:18.938: D/memalloc(4653): /dev/pmem: Mapped buffer base:0x51bf0000 size:9031680 offset:7495680 fd:68
10-09 09:26:19.038: I/System.out(4653): telephonyManager...android.telephony.MSimTelephonyManager@41aefa58
10-09 09:26:19.038: I/System.out(4653): clazz...class android.telephony.MSimTelephonyManager
10-09 09:26:19.038: W/System.err(4653): java.lang.NoSuchMethodException: getITelephony []
10-09 09:26:19.058: W/System.err(4653):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-09 09:26:19.058: W/System.err(4653):     at java.lang.Class.getDeclaredMethod(Class.java:685)
10-09 09:26:19.058: W/System.err(4653):     at             com.example.demo.OutGoingScreen$1.onClick(OutGoingScreen.java:73)
10-09 09:26:19.058: W/System.err(4653):     at android.view.View.performClick(View.java:3567)
10-09 09:26:19.058: W/System.err(4653):     at android.view.View$PerformClick.run(View.java:14224)
10-09 09:26:19.058: W/System.err(4653):     at android.os.Handler.handleCallback(Handler.java:605)
10-09 09:26:19.058: W/System.err(4653):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 09:26:19.058: W/System.err(4653):     at android.os.Looper.loop(Looper.java:137)
10-09 09:26:19.058: W/System.err(4653):     at android.app.ActivityThread.main(ActivityThread.java:4517)
10-09 09:26:19.058: W/System.err(4653):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:26:19.068: W/System.err(4653):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 09:26:19.068: W/System.err(4653):     at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-09 09:26:19.068: W/System.err(4653):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-09 09:26:19.068: W/System.err(4653):     at dalvik.system.NativeStart.main(Native Method)
10-09 09:26:19.068: D/AndroidRuntime(4653): Shutting down VM
10-09 09:26:19.078: W/dalvikvm(4653): threadid=1: thread exiting with uncaught exception (group=0x40c06a68)
10-09 09:26:19.088: E/AndroidRuntime(4653): java.lang.NullPointerException
10-09 09:26:19.088: E/AndroidRuntime(4653):     at com.example.demo.OutGoingScreen$1.onClick(OutGoingScreen.java:79)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at android.view.View.performClick(View.java:3567)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at android.view.View$PerformClick.run(View.java:14224)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at android.os.Handler.handleCallback(Handler.java:605)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at android.os.Looper.loop(Looper.java:137)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at android.app.ActivityThread.main(ActivityThread.java:4517)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-09 09:26:19.088: E/AndroidRuntime(4653):     at dalvik.system.NativeStart.main(Native Method)

I have added this in manifest

 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
 <uses-permission android:name="android.permission.READ_CONTACTS" />
 <uses-permission android:name="android.permission.VIBRATE" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.READ_CALL_LOG" />
 <uses-permission android:name="android.permission.WRITE_CONTACTS" />

my Itelephony.aidl file is

package com.android.internal.telephony;
interface ITelephony {

boolean endCall();

void answerRingingCall();

}
Was it helpful?

Solution

Hi there looks like you are getting NoSuchMethodException here

 method = clazz.getDeclaredMethod("getITelephony");

Its possible that the method is removed form the android framework in the samsumg device.You can test this by trying if the method works in android emulator for android 4.0.4

EDIT: I just checked the source code for Android TelephonyManager.java here. The method does exist in android 4.0.4 source code as well. For me the issue looks like the samsumg device specific. You can test this in another actual device that is running android 4.0.4 to verify. Mine is running 4.3 so cannot test.

OTHER TIPS

The MSimTelephonyManager class from the Samsung device may bear some similarity to the code from Code Aurora -

https://github.com/gp-b2g/frameworks_base/blob/master/telephony/java/android/telephony/MSimTelephonyManager.java

In addition to using getITelephonyMSim instead of getITelephony to get the interface to the Phone, the declaration for endcall under ITelephonyMSim interface so obtained is boolean endCall(int subscription) and so you may need to specify the subscription id of the call to hang up. The aidl file in your project would need to be changed to ITelephonyMSim.aidl and adapted accordingly.

https://github.com/gp-b2g/frameworks_base/blob/master/telephony/java/com/android/internal/telephony/ITelephonyMSim.aidl

An alternative, assuming MSimTelephonyManager extends Android's TelephonyManager (which should be the case as the cast in the first line of your code did not throw an exception), is to call getSuperclass() to get the TelephonyManager class first (or directly get it using Class.forName("android.telephony.TelephonyManager")), and then invoke the original getITelephony, but depending on the implementation this may only work for the default subscription.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top