当我使用带有ResultReceiver的IntentService时,我的Activity被销毁时会发生什么

问题描述:

我通过网络搜索了这个答案。但没有找到结果。
很抱歉,我是Java和Java的新手。 Android编程。

I have searched through the net to find this answer. But no result found. Sorry that I'm a very newbie in Java & Android programming.

我会更详细地阐述我的问题。假设我的Activity启动了一个IntentService,它在后*立运行。该服务订阅到ResultReceiver的回调以更新活动UI。并且在服务执行长时间运行的过程中,Activity被销毁。那么,如果服务通过ResultReceiver向Activity发送回复或进度更新,会发生什么?

I would elaborate on my question more. Let say my Activity started an IntentService and it's running independently in the background. The service is "subscribed" to the callback of ResultReceiver to update the Activity UI. And during the service is executing the long running process, the Activity is destroyed. So what will happen if the service send a reply or progress update to the Activity via the ResultReceiver ?

因为我知道,ResultReceiver需要有一个Activity的引用。

Cause as I know, the ResultReceiver need to have a reference of the Activity.

在我的项目中,我需要开发一个视频剪辑消息应用程序。当用户捕获视频时,它会将数据传递给服务,服务将执行上传以及将一些信息保存到db ..同时继续通过ResultReceiver将进度更新发布到活动UI。

In my project I'm required to develop an video clip messaging app. Which when user captured the video, it will pass the data to the service and the service will perform uploading as well as saving some info into db.. while keep posting the progress update to the Activity UI via ResultReceiver.

用户可以根据需要退出或终止活动。但是当他们导航回app / Activity时,如果上传/下载仍在进行中,则需要显示当前进度..

User is able to quit or kill the Activity as they like. But when they navigate back to the app/Activity, if the upload/download is still ongoing, it need to show the current progress..

最初我想到的是Asynctask,但它也有类似我提到的问题。它需要调用者活动的参考。

Initially I think of Asynctask, but it's also having similar problem as I mentioned. It need a reference of the caller Activity.

我能在哪里达到我提到的要求吗?
对不起,很长的帖子。希望有人可以启发我一点,有一些代码片段甚至更好。
非常感谢:)

Is there anywhere that I can achieve the requirement I mentioned ? Sorry for the long post. Hope someone can enlighten me a bit and it's even better to have some code snippet. Thanks a lot :)

编辑:简而言之,有没有办法将新创建的Activity动态绑定到正在运行的IntentService,以便服务可以将进度更新发送到正确的活动吗?

在您的活动上使用BroadcastReceiver并注册/取消注册它分别在 onResume() onPause()。您可以使用 IntentFilter IntentService $ c $中侦听 Intent C>。

Use a BroadcastReceiver on your Activity and register/unregister it in onResume() and onPause() respectively. You can use an IntentFilter to listen for an Intent from your IntentService.

IntentService 中,为 IntentFilter $ c $创建一个Action值c>并在您想要更新时发送带有该操作的广播:

In your IntentService, create an Action value for the IntentFilter and send a broadcast with that Action when you want to update:

public static final String ACTION = "com.example.yourapp.action.ACTION_TAG";

private void handleResult() {
    if (BuildConfig.DEBUG) {
        Log.v(TAG, "handleResult");
    }

    // Broadcast result
    Intent intent = new Intent(ACTION);
    intent.putExtra("your_extra", someValue);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

活动中

private BroadcastReceiver mYourReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BuildConfig.DEBUG) {
                Log.v(TAG, "onReceive");
            }



            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                // Update with content from bundle
            }
        }
    };

     @Override
     public void onResume() {
         super.onResume();

         if (BuildConfig.DEBUG) {
             Log.v(TAG, "onResume");
         }

         // Register broadcast receiver
         LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourReceiver, new IntentFilter(YourIntentService.ACTION));
    }

    @Override
    public void onPause() {
         super.onPause();
         if (BuildConfig.DEBUG) {
             Log.v(TAG, "onPause");
         }

         // Unregister broadcast receiver
         LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourReceiver);
    }