1

In my application I need to make a call by an intent several times. This is the scenario. 1. User enters a phone number, delay time and number of attempts in the interface. 2. Once user press the dial button in my UI it will make a call and will hang up after the specified delay. 3. Once hung up, the app needs to make a call again until the entered number of attempts is satisfied.

I have used android ITelephony.aidl and java reflection to call hangup function. For one attempt I can successfully terminate the call. The problem occurs when I need to make several attempts. This is my code:

private void sendCommand(String number, int delay, int tries){
    for(int i=0;i<tries;i++){
        callPhone(number);
        delayTimer(delay);
    }
}
private void callPhone(String number){
    String num = "0776355524";
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    if(!(number.equalsIgnoreCase(""))){
        num = number;
    }   
    callIntent.setData(Uri.parse("tel:"+num));
    startActivity(callIntent);
}
private void delayTimer(int delay){
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after delay
            if(isOffhook()){
                cancelCall();
            }

        }
    }, delay);
}

private boolean isOffhook(){
     TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
    Log.v(TAG, "Get getTeleService...");
    Class c = Class.forName(tm.getClass().getName());
    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    telephonyService = (ITelephony) m.invoke(tm);

    return telephonyService.isOffhook();

    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Error in accessing Telephony Manager: "+ e.toString());
         telephonyService = null;
    }

    return false;
}

private void cancelCall(){
    Log.d(TAG, "Ending call..."+ incomingNumber);
    try {
        if(telephonyService != null)
        telephonyService.endCall();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Error in accessing Telephony Manager: "+ e.toString());
    }
}

What am I doing wrong in my code. Thanks in advance, Hasala

T. Kiley
  • 2,752
  • 21
  • 30
Hasala
  • 126
  • 10
  • What precisely it happening that shouldn't be? Does it crash (if so, what is the error) or does it do something you are not expecting? – T. Kiley Nov 10 '13 at 10:57
  • @T.Kiley This is what I do. When I click the Dial Button in my UI I need to dial a number. After specific delay call should hang up. That works now. I need to do it multiple times (I specify how many times) The problem what happens is when the call is terminated it displays the call history instead of my Main Activity. Please help me. Thanks P.S: It does not crash. – Hasala Nov 11 '13 at 05:45

1 Answers1

0

This can't work:

for(int i=0;i<tries;i++){
    callPhone(number);
    delayTimer(delay);
}

This loop calls the number, then posts a Runnable to hangup after some period of time. Then immediately calls the number again. You need to wait for the delay before calling the number again. Do this once (not in a loop):

    callPhone(number);
    delayTimer(delay);

Then, in your Runnable, do this:

        // Do something after delay
        if(isOffhook()){
            cancelCall();
        }
        // now call the number again
        callPhone(number);
        // reschedule the delay
        delayTimer(delay);

You'll have to keep track of the number of tries somewhere, so you know when to stop.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you sir. I did as you described. After the first try, I have to press back button to initiate the second try. Is it possible to launch my Main Activity automatically once the call is terminated? Thanks a lot. – Hasala Nov 12 '13 at 04:49
  • Yes, it is possible. There is a broadcast intent that is sent out when the call is terminated. You can listen for that and launch your activity when you see it. Check http://stackoverflow.com/questions/4925024/is-there-a-way-to-get-notification-whenever-a-call-get-terminated – David Wasser Nov 12 '13 at 08:46