2

By below code I founded incommingCall that is missed or rejected

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

But I only want to found rejected Call?How do it?
Is it possbile to detect rejected calls in Android?
by above link we check calllog for it .
but when we check calllog ; my call not inserted into rejected list yet.
so how?

Community
  • 1
  • 1
Mahdi-bagvand
  • 1,396
  • 19
  • 40
  • @ Harish Godara yes I test this way .But when we check for exist of call in rejected list ;the call not inserted to rejected list yet ? – Mahdi-bagvand Aug 09 '13 at 09:50
  • Are you looking for this : http://stackoverflow.com/questions/4490403/android-is-it-possbile-to-somehow-detect-rejected-calls – krishna Aug 09 '13 at 09:50
  • @krishna yes I test this way .But when we check for exist of call in rejected list ;the call not inserted to rejected list yet ? – Mahdi-bagvand Aug 09 '13 at 09:53

1 Answers1

0

After phone state goes to IDLE, you need to fetch the call log history and get the last entry and check the type. Call it with a 200ms delay using a handler, that will give the system enough time to write the entry.

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

        }

Below is the code to fetch call log history.

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

The POJO object to save call log details.

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;
}
Amal Paul
  • 201
  • 3
  • 5
  • public class ColumnMapper { public static void mapCallLogType(Cursor cursor, CallLogDetail callLogDetail, int idxType) { callLogDetail.type = cursor.getString(idxType); } public static void mapCallLogNumber(Cursor cursor, CallLogDetail callLogDetail, int idxNumber) { callLogDetail.number = cursor.getString(idxNumber); } public static void mapCallLogId(Cursor cursor, CallLogDetail callLogDetail, int idxID) { callLogDetail.id = cursor.getString(idxID); } } – Duna Dec 28 '19 at 11:10