2013-10-29 66 views
2

我需要从后台服务重新启动我的主要活动。
我通过BroadcastReceiver得到我的服务结果,我需要恢复它(在一些操作之后)。从服务中恢复Android活动

可能吗?
我该怎么做?

我试过这个解决方案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(); 
      } 
      } 
     } 

}; 
+0

你是什么意思,它不适合你?你有例外吗?你是否需要应用程序进入前台并完全中断用户的行为? –

+0

没有错误。该应用程序没有恢复,我敢肯定服务正在运行,因为我有一些日志。 – enfix

+0

发布一些代码,以便我们可以看到你在做什么。大量的应用程序从包括我自己在内的通知中启动。这绝对有效,并可能在Android上。 –

回答

4
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); 
1

您必须使用此标志与您的意图:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

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

android:launchMode="singleTask"