Android:应用程序会在启动活动时自动进入前台

时间:2017-10-31 04:03:01

标签: android android-intent android-activity background foreground

我的应用中有一个注册流程。发布成功注册后我启动了一个活动(异步网络呼叫)。在注册过程中需要大约15-20秒,如果我去其他应用程序,我的应用程序将在启动后注册活动时进入前台。

调用signUp片段:

@Override
public void onAttach(Context context) {
        super.onAttach(context);
        activity = getActivity();
    }


   public void navigateToPostSignUp() {
    startActivity(PostSignUpActivity.newIntent(activity, false, false));
    activity.finish();
}
来自PostSignUpActivity的

片段:

public static Intent newIntent(@NonNull Context context,
                                   boolean someBool1,
                                   boolean someBool2) {
        Intent intent = new Intent(context, PostSignUpActivity.class);
        intent.putExtra(BOOL_1, someBool1);
        intent.putExtra(BOOL_2, someBool2);
        return intent;
    }

清单中的片段:

<activity
      android:name=".presentation.ui.activity.PostSignUpActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize" />

1 个答案:

答案 0 :(得分:0)

在邮寄注册后,您可以检查活动是否在后台。如果活动在后台,您可以显示通知或启动您的活动。

您可以使用以下方法检查活动是否在后台。

public boolean isInBackground(){
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses= activityManager.getRunningAppProcesses();     


for (RunningAppProcessInfo appProcess : appProcesses) {

    PackageInfo p = null;
    try {
        p = this.getPackageManager().getPackageInfo(appProcess.pkgList[0], PackageManager.GET_PERMISSIONS);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }



    if(p != null && p.packageName.equals(getPackageName()) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){                                
        Log.d("Is in background:", "false");
        return false;
    }
}

Log.d("Is in background:", "true");
return true;}