使 phone 调用片段页面按钮单击,然后 return 返回相同的片段而无需重新启动 Android 应用程序

Make phone Call fragment Page Button Click, then return back to same Fragment without Restart the Android App

1.I 想让 Phone 从 App Fragment 页面调用 Button clicked its works Fine.

2.after Fragment 调用Page.Its 重启APP

3.I 想要停止 this.Just 需要打开相同的片段页面,而无需重新启动应用程序

我的代码:

private class MyPhoneListener extends PhoneStateListener
{

    private boolean onCall = false;

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

        switch (state)
        {
            case TelephonyManager.CALL_STATE_RINGING:
                // phone ringing...
                //Toast.makeText(FindStores_tabviewonclick_showpage.this, incomingNumber + " calls you", Toast.LENGTH_SHORT).show();
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                // one call exists that is dialing, active, or on hold
                //Toast.makeText(FindStores_tabviewonclick_showpage.this, "on call...", Toast.LENGTH_SHORT).show();
                //because user answers the incoming call
                onCall = true;
                break;

            case TelephonyManager.CALL_STATE_IDLE:
                // in initialization of the class and at the end of phone call

                // detect flag from CALL_STATE_OFFHOOK
                if (onCall == true)
                {
                    //Toast.makeText(Find_stores.this, "restart app after call", Toast.LENGTH_LONG).show();

                    Log.i("AFTER PHONE Call", "restart app");
                    // restart our application
                    Intent restart = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
                    restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //.restart.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(restart);



                    onCall = false;
                }
                break;
            default:
                break;
        }

    }
}

使用这个

getActivity().getFragmentManager().beginTransaction().remove(YourCurrentFragment.this);

更改 TelephonyManager.CALL_STATE_IDLE 中的意图标志:

 Intent restart = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
               restart.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

                startActivity(restart);