问题
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?
回答1:
When you reach ideal after ringing read call logs and get type of call if missed it has a type 3 and if rejected type 5
And try this one also : Is it possbile to detect rejected calls in Android?
回答2:
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;
}
来源:https://stackoverflow.com/questions/18143886/how-found-that-call-rejected-or-missed