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

问题描述 投票:1回答: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;             }
}       
}
}

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

android call
2个回答
2
投票

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

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


0
投票

手机状态进入IDLE后,您需要获取通话记录历史记录并获取最后一个条目并检查类型。使用处理程序以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;
}
© www.soinside.com 2019 - 2024. All rights reserved.