4

I'm trying to develop something similar to AutoAnswer but it also auto hangs up when a broadcast receiver is notified. I've spent all day reading other stackoverflow question on this issue and it seems that the permission MODIFY_PHONE_STATE is restricted to System Apps, however, some posts said that using endCall(); does not require this permission.

My question has two parts:

  1. Is endCall() still usable? Has it ever been used anywhere in any case since Android 2.3?
  2. If it is, I need help making it work because right now it's not working. I download ITelephony.aidl from http://code.google.com/p/autoanswer/source/browse/trunk/src/com/#com/android/internal/telephony and put it in a package. I'm a new user so I can't post images.

This is what my file tree looks like in netbeans:
enter image description here

And this is the code thats running in my broadcastreceiver. It's been posted numerous times here before as a solution to this problem yet I still can't get it to work :/

    ITelephony telephonyService;
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  
    try {
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(telephony);
        telephonyService.endCall();
    } 
    catch (Exception e) {
            //TODO: some exception handling
    }

Looking through the logs, I see this error:

[PhoneIntfMgr] CMD_END_CALL: no call to hang up 

EDIT: I'm brand new to android development and can't seem to find the console output for android apps - like for a call to e.printStackTrace() but I did have Toast display some text inside the catch block like this:

catch (Exception e) {
       Toast toast = Toast.makeText(context, "IN CATCH BLOCK ", Toast.LENGTH_LONG);
       toast.show();
        e.printStackTrace();
    }

But nothing shows up on the screen..

Kara
  • 6,115
  • 16
  • 50
  • 57
  • With the state of your code you can't tell if an exception was thrown or not. Maybe for started you should add some debugging output to your catch block. – Paul Jun 06 '12 at 02:27
  • Hope it Helps.. http://stackoverflow.com/questions/10860369/fetch-dial-number-while-calling – Mehul Jun 06 '12 at 11:31
  • First learn about logcat. Then think about why you want to hang up the user's phone on them - that's very rarely an appropriate thing to do. – Chris Stratton Jun 06 '12 at 17:45

1 Answers1

0

you can use this code :

telephonyManager.listen(new PhoneStateListener() {

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    /* your code */
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Toast.makeText(context.getApplicationContext(), "OFFHOOK",
                            Toast.LENGTH_SHORT).show();
                    setResultData(null);
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    /* your code */
                    break;
                }
                // super.onCallStateChanged(state, incomingNumber);
            }

        }, PhoneStateListener.LISTEN_CALL_STATE);

When you are going to OFFHOOK state, you can see a toast that show "OFFHOOK" and then setResultData(null); is end the call.

AliSh
  • 10,085
  • 5
  • 44
  • 76