How to calculate the outgoing call ( which I dialed from my app) duration in android programmatically

[亡魂溺海] 提交于 2019-12-11 08:19:51

问题


I have given the permission in manifest.xml file

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

And define a receiver:

<receiver android:name=".OutgoingCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

The receiver file is:

public class OutgoingCallReceiver extends BroadcastReceiver {

public static String TAG = "AUTODIAL";
static long start_time, end_time, call_duration;
String number;

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "intent get data : "+intent.getData(), Toast.LENGTH_LONG).show();
    String action = intent.getAction();
    String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    Toast.makeText(context, "Phone State : "+state, Toast.LENGTH_LONG).show();

    if(state==null) {
        number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Toast.makeText(context, "Out going Phone number : "+number, Toast.LENGTH_LONG).show();
    }

    if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            start_time = System.currentTimeMillis();
        }
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            end_time = System.currentTimeMillis();
        }
        call_duration = end_time - start_time;
}
}

Here, it is giving all out going call duration and not only the ones I dialed from my app .

And is it possible to go back to my app after dialing from it?


回答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.



来源:https://stackoverflow.com/questions/44690486/how-to-calculate-the-outgoing-call-which-i-dialed-from-my-app-duration-in-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!