本文介绍了安卓:查询通话记录后它已被更新一次,在通话结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通话结束后,我怎么能肯定的是我查询通话记录后,呼叫信息已经被写入到数据库?

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?

我要寻找一个电话的使用的BroadcastReceiver 与意图过滤器 android.intent.action.PHONE_STATE ,找手机进入空闲状态。

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 .

在这个任何帮助将是AP preciated。

Any help on this would be appreciated.

感谢

推荐答案

下面是非常非常好的答案。

Here is the very very good answer.

请参阅下面的链接

Click在这里,

当你看到上面的例子中,你将学会如何获得受理的最终状态,你也会记住,通话结束后, CALL_STATE_IDLE 将调用超过一次,所以你必须采取一个静态变量在一些地方,你必须在你的理想工作状态,以检查变量的值。

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.

修改

安卓商店通话记录在其内置的数据库信息。因此,更好的解决办法是,当你的code调用 IDLE OFFHOOK 状态的状态,那么你可以复制所有新来电从内置的数据库登录到数据库,为获取通话记录的信息。

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

光标C = context.getContentResolver()查询(android.provider.CallLog.Calls.CONTENT_URI,NULL,NULL,NULL,NULL);

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

EDIT2

EDIT2

下面是完整的例子

这是PhoneStateListener类

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;
}

这是广播接收机类

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);
    }
}

这是函数获取新的通话记录从内部数据库

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

这是清单文件

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

这篇关于安卓:查询通话记录后它已被更新一次,在通话结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 15:15