How force aplication always run in its own task?

2019-04-03 01:47发布

I think that App Manager runs my application after installation in wrong way. It runs my applications in its task. When I press HOME and press application icon I runs second task with my application.

I tested it. I made two applications App1, App2. App2 has two activities A and B. App1 runs App2 in the simplest way.

Intent intent = new Intent(Intent.ACTION_RUN); 
intent.setComponent(new ComponentName("org.app2.test", "org.app2.test.Screen1"));

Test 1. Run App1. App1 runs App2 activity A. Acctivity A runs activity B. Press Home. Press App2 icon. You can see App2 activity A. (Wrong. We have to Tasks with App2)

That I changed code for launching App2.

Intent intent = new Intent(Intent.ACTION_MAIN, null); 
intent.addCategory(Intent.CATEGORY_LAUNCHER); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setComponent(new ComponentName("org.app2.test", "org.app2.test.Screen1"));

Test 2. Run App1. App1 runs App2 activity A. Acctivity A runs activity B. Press Home. Press App2 icon. You can see App2 activity B. (Ok.)

How can I change App2 manifest and force App2 always run in its own task?

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Screen1"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Screen2">
            <intent-filter>
              <action android:name="org.app2.test.screen2" />
              <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
    </activity>

</application>

1条回答
欢心
2楼-- · 2019-04-03 02:01

I detects situation of first run of application and restarts it.

if (first_run) {
  Log.w(TAG, AppHelper.FIRST_RUN);      

  PendingIntent intent = PendingIntent.getActivity(this.getBaseContext(), 0, (new Intent(getIntent())).addCategory(Intent.CATEGORY_LAUNCHER), Intent.FLAG_ACTIVITY_NEW_TASK);

  AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
  mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, intent);
  System.runFinalizersOnExit(true); 
  System.exit(2);

  return;
}
查看更多
登录 后发表回答