简体   繁体   English

启动Activity不会将应用程序置于前台

[英]Starting an Activity does not put application in foreground

I make a phone call from an Activity and when call ends I want to come back to application. 我打电话给一个Activity ,当电话结束时,我想回到应用程序。 I tried all the solutions available on stackoverflow. 我尝试了stackoverflow上提供的所有解决方案。 One of them used to work for few minutes but not working now. 其中一个曾经工作几分钟但现在不工作。

I tried using recreate() method which successfully calls onCreate method of an Activity but app is not in foreground. 我尝试使用recreate()方法成功调用Activity onCreate方法,但app不在前台。 I tried using various flags such as FLAG_ACTIVITY_CLEAR_TOP , FLAG_ACTIVITY_CLEAR_TASK , FLAG_ACTIVITY_NO_HISTORY . 我尝试使用各种标志,如FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_CLEAR_TASKFLAG_ACTIVITY_NO_HISTORY But does not work. 但是不起作用。

Code to go back to application from call app : 从呼叫应用程序返回应用程序的代码:

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

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

        // If call ringing
        if (state == TelephonyManager.CALL_STATE_RINGING) {

        }
        // Else if call active
        else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

            isPhoneCalling = true;
        }
        // Else if call idle
        else if (state == TelephonyManager.CALL_STATE_IDLE) {

            if (isPhoneCalling) {

                isPhoneCalling = false;

                MyActivity.this.recreate();
            }
        }
    }
}

This is my solution, it works perfectly on 4.3 - other OS versions not tested yet, but everything should be fine. 这是我的解决方案,它在4.3 - 其他尚未测试的操作系统版本上完美运行,但一切都应该没问题。

Registering the listener in MainActivity : MainActivity注册监听器:

TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    listener = new ListenToPhoneState();
    tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

The PhoneStateListener from MainActivty : MainActivty的PhoneStateListener:

private class ListenToPhoneState extends PhoneStateListener {

    private String LOG_TAG = "mainactivity";
    private boolean isCallFinished = false;

    public void onCallStateChanged(int state, String incomingNumber) {
        if (TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }
        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // wait for phone to go offhook (probably set a boolean flag) so
            // you know your app initiated the call.
            isCallFinished = true;
            Log.i(LOG_TAG, "OFFHOOK");
        }
        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // when this state occurs, and your flag is set, restart your
            // app
            if (isCallFinished) {
                isCallFinished = false;
                Intent i = new Intent(getApplicationContext(),
                        MainActivity.class);
                // this needs if you want some special action after the phone call 
                //ends, that is different from your normal lauch configuration
                i.setAction("SHOW_PHONE_CALL_LIST");
                startActivity(i);
                finish();
            }
            Log.i(LOG_TAG, "IDLE");
        }
    }

}

I start the phone call from a fragment , but that doesn't make any difference: 我从一个fragment开始打电话,但这没有任何区别:

Uri uri = Uri.parse("tel:" + Uri.encode(callIt));
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
getActivity().finish();

You have to call finish() on the activity that starts the call, otherwise after the phone call, your app will stay behing the default Phone application. 您必须在启动呼叫的活动上调用finish() ,否则在通话后,您的应用将保持默认的电话应用程序。

When your app is starting, you can have a look at the intent , and you can set up your after call configuration. 当您的应用程序启动时,您可以查看intent ,并可以设置后续呼叫配置。 Call this in your onCreate() method: 在你的onCreate()方法中调用它:

if (intent.getAction().equals("SHOW_PHONE_CALL_LIST")) {
        //perfom after call config here
    }

I hope everything is clearly explained. 我希望一切都清楚地解释。

I think you must use a broadcast receiver and start the activity upon uphook. 我认为你必须使用广播接收器并在上升时开始活动。 Also you need to declare permissions to read phone state. 您还需要声明读取电话状态的权限。

  1. Try to modify your code, that the important stuff is not in the onCreate() method but in the onResume() method, because when you come back from the phone call, the onCreate() method will not be triggered but the onResume() method. 尝试修改你的代码,重要的东西不在onCreate()方法中,而是在onResume()方法中,因为当你从电话调用回来时,onCreate()方法不会被触发但是onResume()方法。 See http://developer.android.com/reference/android/app/Activity.html for more details on the Activity live cycle. 有关活动实时周期的更多详细信息,请参阅http://developer.android.com/reference/android/app/Activity.html

  2. If that still does not help, use the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT . 如果仍然没有帮助,请使用标志Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

  3. Try to use a context from the listener. 尝试使用侦听器中的上下文。 Either the listener directly provides a context (so you can just call startActivity(…) or call getApplicationContext().startActivity(…) 侦听器直接提供上下文(因此您只需调用startActivity(…)或调用getApplicationContext().startActivity(…)

In the end your code should something like this: 最后你的代码应该是这样的:

Intent intent = new Intent(RS_HomeScreenActivity.this,RS_HomeScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

The solution is to launch an application with an extra included in bundle that says that app is launched as part of coming back from any other app. 解决方案是启动一个包含extra包含的应用程序,该应用程序表示应用程序是从任何其他应用程序返回的一部分启动的。

When call ends, I launch an app using below code : 通话结束后,我使用以下代码启动应用:

// Launch app
Intent i = new Intent(ActivityThatMadeCall.this,
LauncherActivity.class);
i.putExtra("EXTRA_RETURNED_FROM_CALL_APP", true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);


Then in my launcher activity I check for the extra and if it exists then start the activty that placed call. 然后在我的启动器活动中,我检查extra ,如果它存在,则启动放置调用的激活。 :

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if(getIntent().getExtras().getString("EXTRA_RETURNED_FROM_CALL_APP") != null) {
        startActivity(new Intent(this, ActivityThatMadeCall.class));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM