使用 Intent 标志启动 Activity 服务在启动后停止
Starting Activity With Intent Flags Stops Service After It Starts
在我的应用程序中,有一个 activity 通过
的意图指向另一个
Intent intent = new Intent(MainActivity.this, OtherClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startService(new Intent(MainActivity.this, Service.class));
startActivity(intent);
问题是在意图上设置的标志使得 onStartCommand()
函数在服务 class 中被调用后,onDestroy()
函数立即在同一服务中运行class。我需要在意图上设置标志,以便用户无法返回到 MainActivity。有谁知道我如何在不停止服务的情况下实现这种效果?
在另一个进程中尝试运行该服务。
在清单中添加这个
<service
android:name=".MyService"
android:enabled="true"
android:process=":my_process" />
我最终调用了 finish(),在启动 intents 时使用它,达到了预期的效果。
在我的应用程序中,有一个 activity 通过
的意图指向另一个Intent intent = new Intent(MainActivity.this, OtherClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startService(new Intent(MainActivity.this, Service.class));
startActivity(intent);
问题是在意图上设置的标志使得 onStartCommand()
函数在服务 class 中被调用后,onDestroy()
函数立即在同一服务中运行class。我需要在意图上设置标志,以便用户无法返回到 MainActivity。有谁知道我如何在不停止服务的情况下实现这种效果?
在另一个进程中尝试运行该服务。
在清单中添加这个
<service
android:name=".MyService"
android:enabled="true"
android:process=":my_process" />
我最终调用了 finish(),在启动 intents 时使用它,达到了预期的效果。