什么是preferred方法来调用一个Android活动回从服务线程 [英] What is the preferred way to call an Android Activity back from a Service thread

查看:170
本文介绍了什么是preferred方法来调用一个Android活动回从服务线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发的Andr​​oid应用程序有以下需求:

一个辅助线程开始服务。该线程进行一些处理,并需要从主活动调用并提供一些异步解答相同的活性的影响。

从活动调用的服务是很容易(的IBinder的东西)

我现在的问题是关于正确实施服务的回调。

我是第一次去添加android.os.Handler的活动和处理线程的anwers在MyActivity.handleMessage(消息),但是这需要我给这个处理程序的参考服务。因此,当Android操作系统决定销毁/重建我的活动由于一个方向变化,例如会发生什么情况?请问我的活动生存,因为它是引用(间接)的服务?如果活动破坏/反正重建,发生在服务我的处理程序的参考是什么?

我想我没有使用正确的方法回调从业务线的活动,所以我想知道,如果有人可以点我做的正确的方式。

TIA

解决方案

我preFER使用的。 HTML相对=nofollow> LocalBroadcastManager

下面是为code在活动的一个例子

 的BroadcastReceiver localBroadcastReceiver =新的BroadcastReceiver()
{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        Log.d(BroadcastReceiver的,收到消息+ intent.getAction());
        Log.d(BroadcaseReceiver,接收数据+ intent.getStringExtra(com.my.package.intent.EXTRA_DATA));
    }
};

@覆盖
保护无效的OnStart()
{
    super.onStart();
    最后LocalBroadcastManager localBroadcastManager =
        LocalBroadcastManager.getInstance(本);
    最后的IntentFilter localFilter =新的IntentFilter();
    localFilter.addAction(com.my.package.intent.ACTION_NAME_HERE);
    localBroadcastManager.registerReceiver(localBroadcastReceiver,localFilter);
}

@覆盖
保护无效的onStop()
{
    super.onStop();
    最后LocalBroadcastManager localBroadcastManager =
        LocalBroadcastManager.getInstance(本);
    //确保注销!
    localBroadcastManager.unregisterReceiver(localBroadcastReceiver);
}
 

在别的地方在你的codeBase的(如在完成你的后台线程):

 最后LocalBroadcastManager localBroadcastManager =
    LocalBroadcastManager.getInstance(上下文);
最终意向意图=新的意图(com.my.package.intent.ACTION_NAME_HERE)
intent.putExtra(com.my.package.intent.EXTRA_DATA,yourBackgroundData);
localBroadcastManager.sendBroadcast(意向);
 

您可以,当然,使用 intent.putExtra 添加任何额外的数据,或者使用多个动作来区分广播消息。

I'm currently developing an Android application that has the following needs:

A worker thread started in a Service. This thread does some processing and needs to be invoked from the main Activity and provide some asynchronous answers to the same Activity.

Invoking the Service from the Activity is easy (IBinder stuff)

My question is now about the proper implementation of the service callback.

I was first going to add an android.os.Handler in the Activity and handle the thread's anwers in MyActivity.handleMessage(Message) but this requires that I give this handler's reference to the service. So what happens when the Android OS decides to destroy/recreate my Activity due to an orientation change for example ? Does my activity stay alive as it is referenced (indirectly) in the service ? If the Activity destroyed/rebuilt anyway, what happens to my Handler reference in the Service ?

I guess I'm not using the right method to callback an Activity from a Service thread, so I wanted to know if someone can point me the correct way of doing.

TIA

解决方案

I prefer using LocalBroadcastManager

Here's an example for the code in your Activity:

BroadcastReceiver localBroadcastReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("BroadcastReceiver", "Message received " + intent.getAction());
        Log.d("BroadcaseReceiver", "Received data " + intent.getStringExtra("com.my.package.intent.EXTRA_DATA"));
    }
};

@Override
protected void onStart()
{
    super.onStart();
    final LocalBroadcastManager localBroadcastManager =
        LocalBroadcastManager.getInstance(this);
    final IntentFilter localFilter = new IntentFilter();
    localFilter.addAction("com.my.package.intent.ACTION_NAME_HERE");
    localBroadcastManager.registerReceiver(localBroadcastReceiver, localFilter);
}

@Override
protected void onStop()
{
    super.onStop();
    final LocalBroadcastManager localBroadcastManager =
        LocalBroadcastManager.getInstance(this);
    // Make sure to unregister!!
    localBroadcastManager.unregisterReceiver(localBroadcastReceiver);
}

Anywhere else in your codebase (such as in the completion of your background thread):

final LocalBroadcastManager localBroadcastManager =
    LocalBroadcastManager.getInstance(context);
final Intent intent = new Intent("com.my.package.intent.ACTION_NAME_HERE")
intent.putExtra("com.my.package.intent.EXTRA_DATA", yourBackgroundData);
localBroadcastManager.sendBroadcast(intent);

You can, of course, use intent.putExtra to add any additional data or use multiple actions to differentiate broadcast messages.

这篇关于什么是preferred方法来调用一个Android活动回从服务线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆