即使在应用程序关闭后,每次收到呼叫时都会启动活动?

时间:2013-03-19 07:49:34

标签: android telephonymanager phone-state-listener

在我的应用程序中,如果使用此代码收到呼叫,则会启动一项活动:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (state == TelephonyManager.CALL_STATE_RINGING) {
            incall = true;
        } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            if (incall) {

                c = TestOnDemo.this;
                Intent i = new Intent(TestOnDemo.this,
                        IncallActivity.class);

                startActivity(i);
                incall = false;

            }

        } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            incall = true;
        } else if (state == 0) {
            // Incoming call: Pause music
            incall = true;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

这很好用 - 在通话结束后启动特定的Activity。但有个问题 当我的应用程序没有运行或不在前台并接到电话时。通话结束后,活动再次启动。任何人都可以帮助我。谢谢?

0 个答案:

没有答案