2

状態が Rigging 状態のときに、ログで不在着信の数をカウントします。次に、Ideal 状態の不在着信の数をカウントします。次に、それらを比較します。しかし、理想的な状態では、コールログは同じ数の不在着信を与えます。受信した電話が不在着信かどうかを検証するのに役立つ人がいますか。

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