开发者

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

开发者 https://www.devze.com 2023-04-01 17:22 出处:网络
When a call ends, 开发者_如何学运维how can I be sure that I am querying the Call Log after the call information has been written to the database?

When a call ends, 开发者_如何学运维how can I be sure that I am querying the Call Log after the call information has been written to the database?

I am looking for the end of a call by using a BroadcastReceiver with an intent-filter on android.intent.action.PHONE_STATE , looking for the phone to go idle .

Any help on this would be appreciated.

Thanks


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号