IntentService 完成父 Activity

IntentService finish parent Activity

我在 Android 应用程序中使用名为 "FirstStart" 的 Activity。在用户按下 "register" 按钮后,我启动了一个注册意图服务来在我的服务器和 GCM 上注册用户。 在我的 onClik(View v) 中,我启动了 Intent:

Intent intent = new Intent(this, RegistrationIntentService.class);
        intent.putExtra("user", givenUsername);
        startService(intent);

在此之后,public void onHandleIntent(Intent intent) 启动并从 GCM 获取令牌并将其放入我的服务器。

这个过程需要几秒钟。我不希望用户在正常屏幕上等待,所以我在 FirstStart Activity 中放置了一个进度条。它在 RegistrationIntentService 启动之前调用并启动。

注册意向服务完成后,我想停止 FirstStart 中的进度条并完成注册意向服务和 FirstStart Activity。

那么如何在我的 RegistrationIntentService 结束时完成 FirstStart Activity? 或者有没有办法在 RegistrationIntentService 完成后返回到 FirstStart Activity?

So how can I finish the FirstStart Activity at the end of my RegistrationIntentService?

显然必须在 FirstStart 和 RegistrationIntentService 之间建立通信。实现它的最快方法是:

  • 使用registerReceiver()方法在FirstStart中注册BroadcastReceiver。其次,完成 RegistrationIntentService 中的所有工作后,您唯一需要做的就是从服务中 sendBroadcast()。请记住 IntentFilter 设置了适当的操作,以便从 RegistrationIntentService 发送的广播将匹配在 FirstStart
  • 中注册的 BroadcastReceiver
  • 做与第一个建议基本相同的事情,但使用 LocalBroadcastManager 常规广播和使用 LocalBroadcastManager 的广播之间的区别在于,在后一种情况下,广播消息仅对您可见应用程序的过程。在前者中,广播可能会被不同的应用程序拦截(如果没有限制ofc的权限)
  • 使用一些开源产品,例如 green robot 的 EventBus,它在广播方面在技术上基本上做同样的事情,但使用您创建的自定义对象。

Or is there a way to go back into the FirstStart Activity, when the RegistrationIntentService is finished?

不过,基于广播的通信是让 Activity 与服务对话的方式。

希望有所帮助:)