怎么发现这个电话被拒绝或错过了?

时间:2013-08-09 09:45:47

标签: android call

通过以下代码我创建了错过或拒绝的incommingCall

public class Call_Receiver extends BroadcastReceiver {
public static Context ctx;
@Override
public void onReceive(Context arg0, Intent arg1) {
    ctx = arg0;
    OutgoingIncomingCallListener phoneListener = new OutgoingIncomingCallListener();
    TelephonyManager telephony = (TelephonyManager) ctx
            .getSystemService(Context.TELEPHONY_SERVICE);

    telephony.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);


    }

class OutgoingIncomingCallListener extends PhoneStateListener {
    public boolean myGoal ;//my goal is Call received and (missed or rejected)


    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
         super.onCallStateChanged(state, incomingNumber);
         switch(state){
         case TelephonyManager.CALL_STATE_IDLE:
                Log.d("fff","IDLE");
                if (myGoal == true)
                {
                    //so call is missed or rejected
                }                       

             break;
         case TelephonyManager.CALL_STATE_OFFHOOK:
                myGoal =false;
                Log.d("fff","OFFHOOK");                       
             break;
         case TelephonyManager.CALL_STATE_RINGING:

                Log.d("fff","RINGING");
                myGoal = true; 
             break;             }
}       
}
}

但我只是想发现被拒绝的电话?怎么做? Is it possbile to detect rejected calls in Android?
通过上面的链接,我们检查calllog 但是当我们检查calllog;我的电话尚未插入被拒绝的清单中 又怎样?

2 个答案:

答案 0 :(得分:2)

当您在振铃读取通话记录后达到理想状态并获得呼叫类型(如果错过)它具有类型3并且如果被拒绝类型5

并尝试这个:Is it possbile to detect rejected calls in Android?

答案 1 :(得分:0)

电话状态变为“空闲”后,您需要获取呼叫记录历史记录并获取最后一个条目并检查类型。使用处理程序以200ms的延迟调用它,这将使系统有足够的时间来写入条目。

if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {

            List<CallLogDetail> callLogDetails;
            new Handler().postDelayed(() -> {
                List<CallLogDetail> callLogDetails1 = ReadCallLogs.fetch(context, 1, 0);
               // use this 'type' to check if it is a rejected or missed call
                Log.d("Amal", "Type: " + callLogDetails1.get(0).type);
            },200);

        }

下面是获取呼叫日志历史记录的代码。

public static List<CallLogDetail> fetch(Context context, int limit, int offset, int type) {
    resolver = context.getContentResolver();
    ArrayList<CallLogDetail> callLogDetailLongSparseArray = new ArrayList<>();
    // Create a new cursor and go to the first position
    Cursor cursor = createCursor(limit, offset, type);
    cursor.moveToFirst();
    // Get the column indexes
    int idxNumber = cursor.getColumnIndex(CallLog.Calls.NUMBER);
    int idxType = cursor.getColumnIndex(CallLog.Calls.TYPE);
    int idxID = cursor.getColumnIndex(CallLog.Calls._ID);

    // Map the columns to the fields of the contact
    while (!cursor.isAfterLast()) {

           CallLogDetail callLogDetail = new CallLogDetail();
            // Get data using cursor.getString(index) and map it to callLogDetail object
            ColumnMapper.mapCallLogType(cursor, callLogDetail, idxType);
            ColumnMapper.mapCallLogNumber(cursor, callLogDetail, idxNumber);
            ColumnMapper.mapCallLogId(cursor, callLogDetail, idxID);


            // Add the contact to the collection
            callLogDetailLongSparseArray.add(callLogDetail);


        cursor.moveToNext();
    }
    // Close the cursor
    cursor.close();

    return callLogDetailLongSparseArray;
}

private static Cursor createCursor(int limit, int offset) {

    String sortOrder = CallLog.Calls.DATE + " DESC limit " + limit + " offset " + offset;

     return resolver.query(
            CallLog.Calls.CONTENT_URI,
            new String[]{CallLog.Calls.NUMBER,
        CallLog.Calls.TYPE,
        CallLog.Calls.DATE,
        CallLog.Calls._ID},
            null,
            null,
            sortOrder
    );
}

用于保存呼叫日志详细信息的POJO对象。

public class CallLogDetail {
    public String number;
    public String date;
    public String type;
    public String duration;
    public String name;
    public String simName;
    public String description;
    public String id;
}