2

通过下面的代码,我创建了被遗漏或拒绝的 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;             }
}       
}
}

但是我只想找到rejected Call?怎么办?
是否可以在 Android 中检测被拒绝的呼叫?
通过上面的链接,我们检查它的通话记录。
但是当我们检查 calllog 时;我的电话尚未插入拒绝列表。
又怎样?

4

2 回答 2

4

当您在振铃阅读通话记录后达到理想状态并获得呼叫类型时,如果未接,则类型为 3,如果被拒绝,则类型为 5

也试试这个:是否可以在 Android 中检测被拒绝的呼叫?

于 2013-08-09T09:49:35.903 回答
0

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

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;
}
于 2018-09-08T06:01:31.780 回答