简体   繁体   English

将活动实例传递给IntentService

[英]Passing Activity Instance to IntentService

Im am trying to pass an instance of my activity to an intent service. 我正在尝试将我的活动实例传递给意图服务。 The reason for this is the intent service does a lot of background server communication and if there is an network error or the server returns an error I want to display a pop up message. 原因是意图服务执行大量后台服务器通信,并且如果出现网络错误或服务器返回错误,我想显示一个弹出消息。

When i create the service i use this 当我创建服务时,我会使用它

    Intent service = new Intent(this, SyncService.class);
    Bundle b2 = new Bundle();
    b2.putParcelable(StringsConfig.OBJECT_DELIVERABLES, objects);
    service.putExtras(b2);
    startService(service);

Is there a way to pass an instance of an Activity over to it. 有没有一种方法可以将Activity的实例传递给它。 I also have a method inside the SyncService class that accept an Activity but i dont know how to create an instance of the sync service class, pass the activity over via the method, and then start the sync service. 我在SyncService类内部也有一个接受Activity的方法,但是我不知道如何创建同步服务类的实例,如何通过该方法传递活动,然后启动同步服务。

Any help is appreciated. 任何帮助表示赞赏。

Its not a great idea to pass an Activity instance to an Intent Service. 将Activity实例传递给Intent服务不是一个好主意。 If your long running Background Service needs to show a dialog message, you are much better off modelling it as an Intent. 如果长期运行的后台服务需要显示对话框消息,则最好将其建模为Intent。

Just do: 做就是了:

Intent dialogIntent = new Intent(getApplicationContext(), YourDialogActivity.class);
dialogIntent.putStringExtra(Constants.TITLE, "Your Dialog Title");
dialogIntent.putIntExtra(Constants.MESSAGE, R.string.yourErrorMessageId);
startActivity(dialogIntent);

That way, the service contract is a lot cleaner. 这样一来,服务合同就更加干净了。

The recommended way for an IntentService to communicate to an activity is via BroadcastReceiver. 建议IntentService与活动进行通信的方式是通过BroadcastReceiver。 Take a look at this example: 看一下这个例子:

In the activity that you want your IntentService to communicate with, create a BroadcastReceiver that listens for a specific intent action (a String). 在您要与IntentService进行通信的活动中,创建一个BroadcastReceiver,该侦听器侦听特定的Intent操作(字符串)。 Here my example is called batchProcessReceiver, and listens for the BATCH_PROCESS_RECEIVER action. 在这里,我的示例称为batchProcessReceiver,并监听BATCH_PROCESS_RECEIVER操作。 BATCH_PROCESS_RECEIVER can be a public static constant in your Activity. BATCH_PROCESS_RECEIVER可以是您的Activity中的公共静态常量。

private BroadcastReceiver batchProcessReceiver = new BroadcastReceiver() {
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BATCH_PROCESS_RECEIVER)) {
            // do what you need to do here
        }
    }
};

In your activity's onResume: 在您活动的onResume中:

registerReceiver(batchProcessReceiver, new IntentFilter(BATCH_PROCESS_RECEIVER));

onPause: onPause:

unregisterReceiver(batchProcessReceiver);

Then at a point in your IntentService, you can do 然后,在您的IntentService中,您可以

sendBroadcast(new Intent(MyActivity.BATCH_PROCESS_RECEIVER));

to trigger the action you want to do in your activity. 触发您要在活动中执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM