Android: Querying Call Log After it has Been Updated Once a Call Ends

橙三吉。 提交于 2019-12-03 12:49:56
Dharmendra

Here is the very very good answer.

See below link

Click Here

When you see above example you will learn how to get call's end-state and you will also keep in mind that after call ends CALL_STATE_IDLE will calls more than once so you have to take one static variable in some where and you have to check that variable value before you work in ideal state.

EDIT

Android stores call log information in its inbuilt database. So better solution is that when your code calls IDLE state after OFFHOOK state then you can copy all newly call log from inbuilt database to your database for get information of call log

You can retrieves call-log information from inbuilt database using following query

Cursor c = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, null);

EDIT2

Here is complete example

This is PhoneStateListener class

public class CustomPhoneStateListener extends PhoneStateListener {

Context context;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);

switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        // Toast.makeText(context, "CALL_STATE_IDLE", Toast.LENGTH_LONG).show();
        if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) {
            UDF.fetchNewCallLogs(context);
        } 
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
         //Toast.makeText(context, "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
         //Toast.makeText(context, "CALL_STATE_RINGING", Toast.LENGTH_LONG).show();
        endCallIfBlocked(incomingNumber);
        break;

    default:
        break;
}
UDF.phoneState = state;
}

This is broadcast receiver class

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        //UDF.createTablesIfNotExists(context);
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
    }
}

This is function for get new call logs from internal database

public static void fetchNewCallLogs(Context context) {

        CallLogHelper callLogHelper = new CallLogHelper(context);
        callLogHelper.open();
        Long maxId = callLogHelper.getMaxId();

        Cursor c = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, "_id > ?", new String[]{String.valueOf(maxId)}, null);
        if(c != null && c.moveToFirst()) {
            while (c.isAfterLast() == false) {
                int _ID = c.getColumnIndex(android.provider.CallLog.Calls._ID);
                int _NUMBER = c.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
                int _DATE =  c.getColumnIndex(android.provider.CallLog.Calls.DATE);
                int _DURATION =  c.getColumnIndex(android.provider.CallLog.Calls.DURATION);
                int _CALLTYPE =  c.getColumnIndex(android.provider.CallLog.Calls.TYPE);
                int _NAME =  c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
                int _NUMBERTYPE =  c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
                int _NEW = c.getColumnIndex(android.provider.CallLog.Calls.NEW);

                String id = c.getString(_ID);
                String number = c.getString(_NUMBER);
                String date = c.getString(_DATE);
                String duration = c.getString(_DURATION);
                String callType = c.getString(_CALLTYPE);
                String name = c.getString(_NAME);
                String numberType = c.getString(_NUMBERTYPE);
                String _new = c.getString(_NEW);

                callLogHelper.createLog(id, number, date, duration, callType, name, numberType, _new, "N");

                c.moveToNext();
            }
        }
        callLogHelper.close();
    }


**Where** 


 => CallLogHelper is a helper class to communicate with my local database
    => callLogHelper.getMaxId(); will returns the maximum id of call logs in my local database and I am keeping the id in local database and internal database will be same
    => callLogHelper.createLog() is my function to insert call log in my local database

This is manifest file

<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE"/>     
</intent-filter>
</receiver>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!