1

我创建了应用程序,用户可以在其中单击按钮进行呼叫。

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

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;
                }

            }
        }
    }

我想要一个代码来检查电话是否与我的应用程序相关,如果我的应用程序完成了电话通话,那么在结束电话通话后它将返回我的应用程序活动,否则不要返回我的活动,默认执行.

谢谢,

4

1 回答 1

1

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

在您的 PhoneListener 中,您可以检查该号码是否与您从 Activity 传递的号码匹配。如果为真,请重新启动您的活动,否则什么也不做。

编辑:

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;
            }
        }
    }
}

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

于 2013-10-04T07:13:39.907 回答