如何在电话呼叫时停止哨子检测

时间:2014-07-12 10:32:03

标签: android

我想在手机呼叫模式时停止哨子检测。当我呼叫应用程序时启用模式如何禁用通话时间。我是android新手。 我的代码在那里..谢谢..

public class CallHelper {
    int prev_state = 0;

    /**
     * Listener to detect incoming calls.
     */
    private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone
                Intent intent = new Intent(ctx, WhistleService.class);
                intent.putExtra("action", "stop");

                ctx.startService(intent);
                Toast.makeText(ctx, "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                prev_state = state;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("TAG", "CALL_STATE_OFFHOOK");
                Intent intent1 = new Intent(ctx, WhistleService.class);
                intent1.putExtra("action", "stop");

                ctx.startService(intent1);
                prev_state = state;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("TAG", "CALL_STATE_IDLE==>");

                if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
                    prev_state = state;
                    // Answered Call which is ended
                    Intent intent11 = new Intent(ctx, WhistleService.class);
                    intent11.putExtra("action", "stop");

                    ctx.startService(intent11);
                }
                if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                    prev_state = state;
                    // Rejected or Missed call
                    Intent intent2 = new Intent(ctx, WhistleService.class);
                    intent2.putExtra("action", "stop");

                    ctx.startService(intent2);
                }
                break;
            }
        }
    }

    /**
     * Broadcast receiver to detect the outgoing calls.
     */
    public class OutgoingReceiver extends BroadcastReceiver {
        public OutgoingReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            Toast.makeText(ctx, "Outgoing: " + number, Toast.LENGTH_LONG)
                    .show();
        }

    }

    private Context ctx;
    private TelephonyManager tm;
    private CallStateListener callStateListener;

    private OutgoingReceiver outgoingReceiver;

    public CallHelper(Context ctx) {
        this.ctx = ctx;

        callStateListener = new CallStateListener();
        outgoingReceiver = new OutgoingReceiver();
    }

    /**
     * Start calls detection.
     */
    public void start() {
        tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        IntentFilter intentFilter = new IntentFilter(
                Intent.ACTION_NEW_OUTGOING_CALL);
        ctx.registerReceiver(outgoingReceiver, intentFilter);
    }

    /**
     * Stop calls detection.
     */
    public void stop() {
        tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        ctx.unregisterReceiver(outgoingReceiver);
    }
}

0 个答案:

没有答案