【问题标题】:How force aplication always run in its own task?强制应用程序如何始终在自己的任务中运行?
【发布时间】:2011-10-02 17:50:01
【问题描述】:

我认为 App Manager 在安装后以错误的方式运行我的应用程序。它在其任务中运行我的应用程序。当我按下 HOME 并按下应用程序图标时,我会使用我的应用程序运行第二个任务。

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

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

测试 1. 运行 App1。 App1 运行 App2 活动 A。活动 A 运行活动 B。按 Home。按 App2 图标。您可以看到 App2 活动 A。(错误。我们必须使用 App2 执行任务)

我更改了启动 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。活动 A 运行活动 B。按 Home。按 App2 图标。可以看到 App2 活动 B。(好的。)

如何更改 App2 清单并强制 App2 始终在自己的任务中运行?

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

【问题讨论】:

    标签: android android-manifest


    【解决方案1】:

    我检测到应用程序首次运行的情况并重新启动它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2015-06-14
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      相关资源
      最近更新 更多