0

I'm working on an android application in order to calling some phone numbers, sequentially. For example, there are 3 editText that each of them contains a specific phone number and after calling the first number, a delay will occur before calling the second number, and so on.

But when I start calling, I see (isn't app responding) message and there is no log in (logcat). What is the solution? The codes are as follows:

  private void onClickCall() {
        check = true;
        sumSms = 0;
          delayCall = Integer.parseInt(editTextDelayCall.getText().toString());
        repeatCall = Integer.parseInt(editTextRepeatCall.getText().toString());
        for (int j=0;j<phoneNumberList.size();j++)
        for (int i = 0; i <repeatCall; i++) {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumberList.get(i).getPhoneNumber()));
            startActivity(intent);
            try {
                Thread.sleep(delayCall);
                endCall(context);

            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                Log.e("0", ie + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

And end call method :

 private void endCall(Context context) throws Exception {

        String serviceManagerName = "android.os.ServiceManager";
        String serviceManagerNativeName = "android.os.ServiceManagerNative";
        String telephonyName = "com.android.internal.telephony.ITelephony";
        Class<?> telephonyClass;
        Class<?> telephonyStubClass;
        Class<?> serviceManagerClass;
        Class<?> serviceManagerNativeClass;
        Method telephonyEndCall;
        Object telephonyObject;
        Object serviceManagerObject;
        telephonyClass = Class.forName(telephonyName);
        telephonyStubClass = telephonyClass.getClasses()[0];
        serviceManagerClass = Class.forName(serviceManagerName);
        serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
        Method getService = serviceManagerClass.getMethod("getService", String.class);
        Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
        Binder tmpBinder = new Binder();
        tmpBinder.attachInterface(null, "fake");
        serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
        IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
        Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
        telephonyObject = serviceMethod.invoke(null, retbinder);
        telephonyEndCall = telephonyClass.getMethod("endCall");
        telephonyEndCall.invoke(telephonyObject);

    }
Firouziam
  • 777
  • 1
  • 9
  • 31
smrt 98
  • 3
  • 5

2 Answers2

0

NEVER CALL SLEEP ON THE UI THREAD. That will block the thread, causing your app to be unresponsive. Do not do it.

I don't know what you're trying to do with that sleep, wait for the call to finish? That won't work. Ever. If you're waiting for the call to finish, look for the phone state to change to ringing then back to off hook. But you can't do it by sleeping, it will be an asynchronous event via broadcast receiver.

And I'm not sure what endcall() is supposed to do, but ending a call from any app other than the dialer is NOT possible in Android. Every way that's been made, Google has broken in the next release. It is not supported functionality.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • You don't do it that way at all. You can't end a call from another app. You can listen for a call to end, but you need to request phone state permission, register a BroadcastReceiver for it, and listen for a transition back to off_hook. By the way stating that intent won't actually start a call, it will only launch the dialer with the number filled in, the user will still need to start it. To start a call directly you'd need to be a system app. – Gabe Sechan Aug 15 '18 at 06:50
  • yes I delay util next call with Thread.sleep() , i know isn`t best way becuase I don`t know any way. I tested the endCall() on five different phone and that work – smrt 98 Aug 15 '18 at 06:55
  • You look at the change in PhoneState. I have some old code https://stackoverflow.com/questions/15563921/how-to-detect-incoming-calls-in-an-android-device/15564021#15564021 that detects ends of incoming and outgoing calls. – Gabe Sechan Aug 15 '18 at 06:56
0

I think what you have to do is first add these 2 permissions in your manifest :

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

1st for call and 2nd for reading the phone state like, if you are calling some one than second call should not be performed after completing the first call end you should perform the second call. I think you should use sychronised thread to call one person after another in sequence. and handle the listeners as well.

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49
  • That won't work, for a variety of reasons. Mainly, you can't actually make a call programatically unless you're a system app- all you can do is launch the dialer with a prefilled number. Notice that CALL_PHONE is privlidged. – Gabe Sechan Aug 15 '18 at 06:54