3

I am making an application to call multiple numbers.

In that app

  1. When I call to 1 person and if the call is answered by the user then the loop should be stopped.

  2. But If the call is rejected then the call should be on next number and loop should be couninue.

My problem is I cant detect whether the call is rejected or answered. when I had search on net some people says it is not possible to detect the call is answered or rejected.

Is it really not possible to detect the call in android If it is possible then how can I do that?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Pooja Roy
  • 545
  • 5
  • 25
  • http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected – Venu Apr 26 '14 at 06:06
  • I already used this but this is only for check the call is ringing or not. But not for check the call is rejected or answered? – Pooja Roy Apr 26 '14 at 06:13

2 Answers2

2

I think you can check outgoing call time of last call in PhoneStateListener class' onCallStateChanged method. Fetch the data if state is idle that is TelephonyManager.CALL_STATE_IDLE.

Something like this:

Cursor mCallCursor = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null,null,null,null);
int duration = mCallCursor.getColumnIndex( CallLog.Calls.DURATION);
while(mCallCursor.moveToFirst())
{   
    Toast.makeText(context, mCallCursor.getString(duration), Toast.LENGTH_LONG).show();
}

You can find more about that here. I haven't tested the above code. But something like that should work.

You can check if time's 00:00, then call next number of loop. Else you can stop calling.

Hope this helps you.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
1

below is a code of detecting outgoing call by accessibility events -

Add a class which extends AccessibilityService in your projects -

public class CallDetection extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
     acquireLock(this);
    Log.d("myaccess","after lock");
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
        Log.d("myaccess","in window changed");
        AccessibilityNodeInfo info = event.getSource();
        if (info != null && info.getText() != null) {
            String duration = info.getText().toString();
            String zeroSeconds = String.format("%02d:%02d", new Object[]{Integer.valueOf(0), Integer.valueOf(0)});
            String firstSecond = String.format("%02d:%02d", new Object[]{Integer.valueOf(0), Integer.valueOf(1)});
            Log.d("myaccess","after calculation - "+ zeroSeconds + " --- "+ firstSecond + " --- " + duration);
            if (zeroSeconds.equals(duration) || firstSecond.equals(duration)) {
                Toast.makeText(getApplicationContext(),"Call answered",Toast.LENGTH_SHORT).show();
               // Your Code goes here
            }
            info.recycle();
        }
    }
}


@Override
protected void onServiceConnected() {
    super.onServiceConnected();
    Toast.makeText(this,"Service connected",Toast.LENGTH_SHORT).show();
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    info.notificationTimeout = 0;
    info.packageNames = null;
    setServiceInfo(info);
}

@Override
public void onInterrupt() {

}
}

But to get the function event.getSource() working you have to specify some of your service configuration through xml, so create a xml folder in your project and add a xml file called serviceconfig.xml (you can give any name you want.

The content of serviceconfig is below -

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/callDetection"
android:accessibilityEventTypes="typeWindowContentChanged"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>

You can find more about serviceconfig in Here

Now add your service in you Manifest file like this -

<service android:name=".CallDetection"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="@string/callDetection">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/serviceconfig" />
</service>

And youre done, just run the app and go to Accessibility settings in your phone, you will find an option named as detection (or whatever name you have given as your service description), switch that on to give accesibility permissions for you app.

Now you will see a toast when call is answered.

you can Code any code you want in there, also you can call a callback function in your activity

Most important - Dont call your call window(android dialer window) untill the call is answered, otherwise this will not work.

Note - As android doesn't provide any solution to detect if the call is answered or not, this is the best alternative i have made, hope it works for you.

kaustav07
  • 284
  • 3
  • 17
  • Please add a specific example of how to apply your solution to resolve this question's specific case. Your answer as it stands is little more than a comment with a link to another question's answer. If that question or answer is removed, this (and the other place(s) you posted an almost identical answer) will be rendered effectively useless. – Fred Gandt Jul 11 '17 at 22:54
  • previously i added the whole code and example to more than one answer, but one moderator from stackoverflow removed my answer saying not to add duplicate ans and that's why i added the link here and actual ans to one question only, suggest me what should i do and i will. – kaustav07 Jul 13 '17 at 16:33
  • I believe _duplicate answers_ are only considered a problem if they are **exact** duplicates. Since there are differences between the questions, and there should therefore be differences in the answers, I personally (as a lowly commoner) would prefer to see complete and context relevant answers instead of (potentially breakable) links to somewhat relevant answers elsewhere. – Fred Gandt Jul 13 '17 at 22:35
  • added full answer – kaustav07 Jul 18 '17 at 15:42