如何力aplication总是在自己的任务中运行?自己的、是在、任务、何力

2023-09-06 01:09:41 作者:洒脱

我认为,应用程序管理器运行安装在错误的道路后,我的应用程序。它运行在它的任务我的应用程序。当我preSS HOME和preSS应用程序图标我运行的第二个任务我的申请。

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.

我测试了它。我做了两个应用程序App1,App2的。 App2的有两个活动A和B App1的运行App2的最简单的方法。

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

测试1.运行程序App1。 APP1运行App2的活动A. Acctivity A运行活动B. preSS家。 preSS App2的图标。你可以看到App2的活动A.(错了。我们必须用App2的任务)

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)

这是我改变code发动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"));

测试2.运行程序App1。 APP1运行App2的活动A. Acctivity A运行活动B. preSS家。 preSS App2的图标。你可以看到App2的活动B.(OK)。

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

如何更改App2的清单,并强制App2的总是自己的任务中运行?

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>

推荐答案

我检测应用程序第一次运行情况,并重新启动它。

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