从服务中恢复Android活动

3
我需要从后台服务重新启动我的主活动。我通过BroadcastReceiver获得我的服务结果,我需要在执行一些操作后恢复它。
这可能吗?我该怎么做?
我尝试了这个解决方案Resume application and stack from notification,但对我不起作用。
编辑:
这是我的接收器:
private BroadcastReceiver receiver = new BroadcastReceiver() {


        @Override
        public void onReceive(Context context, Intent intent) {
          Bundle bundle = intent.getExtras();
          if (bundle != null) {
            int resultCode = bundle.getInt(DownloadService.RESULT);
            if (resultCode == RESULT_OK) {
                Log.i("MyApp", "RESULT OK");
                final Intent mainIntent = new Intent(context, MainActivity.class);
                mainIntent.setAction(Intent.ACTION_MAIN);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(mainIntent);
            } else {
              Toast.makeText(MainActivity.this, "KO",Toast.LENGTH_LONG).show();
            }
          }
        }

 };

你说它对你不起作用是什么意思?你是否遇到了异常?你需要将应用程序调入前台并完全中断用户正在进行的操作吗? - Matt Wolfe
没有错误。应用程序没有恢复,我确定服务正在运行,因为我有一些日志。 - enfix
发布一些代码,这样我们就可以看到你在做什么。包括我的应用在内,大量应用程序都可以从通知中启动。这在安卓系统上绝对可行。 - Matt Wolfe
2个回答

6
Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.getApplicationContext().startActivity(intent);

4

您必须在Intent中使用此标志:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

如果您不想要一个新的 Activity 实例,请在 AndroidManifest 文件中为您的 Activity 设置 launchMode:

android:launchMode="singleTask"

android:launchMode="singleTask" 对我有用 - Red wine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接