在开发中会遇到需要判断用户拨打电话,对方是不是接通成功了。


解决方案:
其实很简单,只需要去系统的call_log表里去查询一下,本次的通话持续时间是否大于0,即可。
代码如下:

private boolean getCallLogState() {
    ContentResolver cr = getContentResolver();
    final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,
                             new String[]{CallLog.Calls.NUMBER,CallLog.Calls.TYPE,CallLog.Calls.DURATION},
                             CallLog.Calls.NUMBER +"=? and "+CallLog.Calls.TYPE +"= ?",
                             new String[]{mobile.getText().toString(),"CallLog.Calls.TYPE"},null);
    while(cursor.moveToNext()){
        int durationIndex = cursor.getColumnIndex(CallLog.Calls.DURATION);
        long durationTime = cursor.getLong(durationIndex);
        if(durationTime > 0){
                isLink = true;
            } else {
                isLink = false;
        }
    }
    return false;
}

如果对方没有接听,则持续时间为0,即使听筒中出现忙音或者提示语,这个表中的记录值都为0。

代码中的 CallLog.Calls.TYPE 请自行选择,枚举值如下

CallLog.Calls.TYPE
//来电:1,拨出:2,未接:3 
public static final int INCOMING_TYPE = 1;   
public static final int OUTGOING_TYPE = 2;   
public static final int MISSED_TYPE = 3;