每次结束电话呼叫回到我的活动,即使我没有从我的应用程序android打电话

时间:2013-10-04 06:57:36

标签: android android-intent

我创建了一个应用程序,用户可以在按钮点击时拨打电话。

我发现下面的代码可以正常拨打电话并在电话结束时返回我的活动。但我在这个应用程序中有一个问题,一旦我从我的应用程序拨打电话并结束该电话,完成整个周期后,我有按下主页按钮或后退按钮。我将从我的电话簿拨打一个电话,当结束通话时,它将返回我的应用程序,而不是电话簿。

public void imgbtnCallPhone_Click(View view) {
    EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone);

        try
        {
           final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL);
           callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText()));
           startActivity(callIntent);
        } catch (ActivityNotFoundException activityException) {
            //Log.e("Calling a Phone Number", "Call failed", activityException);
        }

}

private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;

        String LOG_TAG = "LOGGING 123";

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                //Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                //Log.i(LOG_TAG, "OFFHOOK");

                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                // run when class initial and phone call ended, 
                // need detect flag from CALL_STATE_OFFHOOK
                //Log.i(LOG_TAG, "IDLE");

                if (isPhoneCalling) {

                    //Log.i(LOG_TAG, "restart app");

                    // restart app
                    Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                            getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

                    isPhoneCalling = false;
                }

            }
        }
    }

我想要一个可以检查电话是否与我的应用程序相关的代码, 如果我的应用程序完成了电话,那么在结束通话结束后它将会回来 我的应用程序活动,否则不会回到我的活动,默认情况下。

谢谢,

1 个答案:

答案 0 :(得分:1)

我认为您需要在致电之前启动PhoneCallListener,将您要调用的号码传递给PhoneCallListener,然后启动callIntent。

在您的PhoneListener中,您可以检查该号码是否与您从活动传递的号码相匹配。如果是,请重新启动您的活动,否则不执行任何操作。

编辑:

public void imgbtnCallPhone_Click(View view) {
    EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone);

    // Get your PhoneCallListener and pass the number
    PhoneCallListener mPhoneListener = new PhoneCallListener();
    mPhoneListener.yourActivity = true;

    // start listening
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    try
    {
       final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL);
       callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText()));
       startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
        //Log.e("Calling a Phone Number", "Call failed", activityException);
    }

}

您的PhoneCallListener:

private class PhoneCallListener extends PhoneStateListener {
    private boolean isPhoneCalling = false;
    public Boolean yourActivity = false;

    String LOG_TAG = "LOGGING 123";

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            //Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            //Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended, 
            // need detect flag from CALL_STATE_OFFHOOK
            //Log.i(LOG_TAG, "IDLE");

            if (isPhoneCalling && yourActivity) {

                //Log.i(LOG_TAG, "restart app");
                // restart app
                Intent i = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(
                        getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                yourActivity = false;
                isPhoneCalling = false;
            }
        }
    }
}

我没有测试它,所以它可能包含一些错误。