安装后启动应用程序,存在不同的行为(安卓)



我有一个应用程序,它有2个活动。

<activity android:name=".LauncherActivity"
          android:theme="@style/LauncherTheme"
          android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
<activity android:name="MainActivity"
          android:launchMode="singleTop"
          android:screenOrientation="portrait"/>

1.(我期望)
通过命令行安装应用程序(ADB Install -R应用)。
点击应用程序图标打开它,发射力显示,然后我对主动脉的启动性,Main Activity显示。
点击回家,然后再次点击应用程序图标,MainActivity再次显示。

2.(异常?)
通过PackageInstaller安装应用程序。
安装完成后,点击"打开"按钮,在packageinstaller中,发射器显示,然后我开始进行主动,主动脉显示。
点击主页,然后再次点击应用程序图标,再次显示发射器!

在我的发射器中

private void startMainActivity() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

我以maunchintent

来调查souce,packageinstaller启动活动

installappprogress.java

mLaunchIntent = getPackageManager().getLaunchIntentForPackage(mAppInfo.packageName);

applicationpackagemanager.java

@Override
public Intent getLaunchIntentForPackage(String packageName) {
    // First see if the package has an INFO activity; the existence of
    // such an activity is implied to be the desired front-door for the
    // overall package (such as if it has multiple launcher entries).
    Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
    intentToResolve.addCategory(Intent.CATEGORY_INFO);
    intentToResolve.setPackage(packageName);
    List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
    // Otherwise, try to find a main launcher activity.
    if (ris == null || ris.size() <= 0) {
        // reuse the intent instance
        intentToResolve.removeCategory(Intent.CATEGORY_INFO);
        intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
        intentToResolve.setPackage(packageName);
        ris = queryIntentActivities(intentToResolve, 0);
    }
    if (ris == null || ris.size() <= 0) {
        return null;
    }
    Intent intent = new Intent(intentToResolve);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(ris.get(0).activityInfo.packageName, ris.get(0).activityInfo.name);
    return intent;
}

我没有信息活动,因此目的是:

Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage(packageName);

我很困惑。为什么有区别行为?帮助!

不确定这是否对您来说仍然是一个问题,但是我通过将其包括在我的Mainactivity中来解决:

if (!isTaskRoot()) {
    final Intent intent = getIntent();
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(intent.getAction())) {
        finish();
        return;
    }
}

这是因为根据如何启动该应用程序(通过安装屏幕与Open vs Launcher打开)发出不同的意图。

最新更新