识别android中的未接来电并向该号码发送消息

时间:2012-12-11 03:08:21

标签: android

当状态处于Rigging状态时,我计算日志中未接来电的数量。然后计算理想状态的未命中呼叫数。然后比较它们。但是在理想状态下,呼叫记录会给出相同数量的未接来电。有人可以帮我验证收到的电话是未接来电还是不来电。

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

}

0 个答案:

没有答案