2

I count the number of missed calls in the log when state is in the Rigging state. Then Count the number of miss call of the Ideal state. Then compare them. But in the ideal state call log is give the same number of miss call. Can some one help me to validate a to recieved call is a miss call or a not.

public class CustomPhoneStateListener extends PhoneStateListener {

Context context;
String callState;
private static boolean wasRiging;
private static int previousNoOfMissCall;
private static int UDF;

public CustomPhoneStateListener(Context context) {
    super();
    this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        callState = "IDEAL";
        if (UDF != TelephonyManager.CALL_STATE_IDLE) {
            sendSMSToMissNo(incomingNumber, "Test");
        }
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        callState = "OFFHOOK";
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        callState = "RIGING";
        previousNoOfMissCall = this.getMisscallCount();
        wasRiging = true;
        break;
    default:
        break;
    }
    UDF = state;
    Log.i(">>>Broadcast", "onCallStateChanged " + callState);
}

public int getMisscallCount() {
    String[] projection = { CallLog.Calls.CACHED_NAME,
            CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
    String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;

    Cursor c = context.getContentResolver().query(
            CallLog.Calls.CONTENT_URI, projection, where, null, null);
    c.moveToFirst();
    Log.d("CALL", "" + c.getCount()); // do some other operation
    return c.getCount();
}

private void sendSMSToMissNo(String phoneNumber, String message) {
    if (this.validateMissCall(previousNoOfMissCall)) {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}

private boolean validateMissCall(int preNoOfMissCall) {
    int crtNoOfMissCall = this.getMisscallCount();
    Log.d("CALL", "" + "In validate"+crtNoOfMissCall);
    if (preNoOfMissCall == crtNoOfMissCall) {
        return false;
    }
    return true;
}
}

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}

}
4

0 回答 0