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

时间:2013-07-16 06:51:05

标签: android android-activity phone-state-listener

我从Activity拨打电话,当通话结束时,我想回到应用程序。我尝试了stackoverflow上提供的所有解决方案。其中一个曾经工作几分钟但现在不工作。

我尝试使用recreate()方法成功调用onCreate的{​​{1}}方法但app不在前台。我尝试使用各种标记,例如ActivityFLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_CLEAR_TASK。但是不起作用。

从通话应用程序返回应用程序的代码:

FLAG_ACTIVITY_NO_HISTORY

4 个答案:

答案 0 :(得分:1)

这是我的解决方案,它在4.3 - 其他尚未测试的操作系统版本上完美运行,但一切都应该没问题。

MainActivity注册听众:

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

来自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");
        }
    }

}

我从fragment开始拨打电话,但这没有任何区别:

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

您必须在启动通话的活动上拨打finish(),否则在通话后,您的应用将保持默认的电话应用程序。

当您的应用启动时,您可以查看intent,然后您可以设置您的通话后配置。请使用onCreate()方法调用此方法:

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

我希望一切都清楚地解释。

答案 1 :(得分:0)

我认为你必须使用广播接收器并在上升时开始活动。您还需要声明读取电话状态的权限。

答案 2 :(得分:0)

  1. 尝试修改代码,重要的东西不在onCreate()方法中,而是在onResume()方法中,因为当你从电话回来时,onCreate()方法不会触发但是onResume()方法。有关活动实时周期的详细信息,请参阅http://developer.android.com/reference/android/app/Activity.html

  2. 如果仍然无效,请使用标记Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

  3. 尝试使用侦听器中的上下文。收听者直接提供上下文(因此您只需拨打startActivity(…)或致电getApplicationContext().startActivity(…)

  4. 最后你的代码应该是这样的:

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

答案 3 :(得分:0)

解决方案是启动一个包含extra的应用程序,该应用程序将该应用程序作为从任何其他应用程序返回的一部分启动。

当通话结束时,我使用以下代码启动应用程序:

// 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);


然后在我的启动器活动中,我检查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));
    }
}
相关问题