检测拨出电话android的状态

时间:2013-02-11 17:05:30

标签: android

我搜索了两天,但没有找到

是否可以通过编程方式检测拨出电话的状态(应答,忙碌或下垂)? 谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

*// Add receiver to manifest file

public class OutgoingCallReceiver extends BroadcastReceiver {
    private static long timeStarted = -1L; // IMPORTANT!

    private static String number;
    private static boolean noCallListenerYet = true;

    @Override
    public void onReceive(final Context context, Intent intent) {
        PhoneCallListener phoneListener = new PhoneCallListener(context);
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class PhoneCallListener extends PhoneStateListener {
        Context context;
        private boolean isPhoneCalling = false;

        public PhoneCallListener(Context context2) {
            // TODO Auto-generated constructor stub
            context=context2;
        }

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //do something here
            }

            if (state == TelephonyManager.CALL_STATE_IDLE && timeStarted != -1L) {}
        }
    }
}