与多个来电Android的共同任务异步 [英] Android common async task with multiple callers

查看:167
本文介绍了与多个来电Android的共同任务异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有开始使用我的TCP客户端类TCP连接的共同任务异步类。而我的主要活动将启动,并且我确定活动为来电我执行这个任务异步。这里是我的异步类code:

I have a common async task class that starts a tcp connection using my tcp client class. I am executing this async task while my main activity is launched and I define activity as caller. Here is my async class code:

public class CommonAsyncTask extends AsyncTask<Handler,String,String> {

    OnAsyncRequestComplete caller;
    Context context;
    String m;
    List<NameValuePair> parameters = null;
    ProgressDialog pDialog = null;
    public TCPClient client;

    public CommonAsyncTask(Activity a) {
        caller = (OnAsyncRequestComplete) a;
        context = a;
    }

    public interface OnAsyncRequestComplete {
        public void asyncResponse(String m);
    }

    @Override
    protected String doInBackground(Handler... params) {
        client = new TCPClient(new TCPClient.OnMessageReceived() {
            @Override
            public void messageReceived(String message) {
                caller.asyncResponse(message);
            }
        });
        client.run();
        return null;
    }
}

在消息中收到我称之为 caller.asyncResponse(消息),它给出的活动运行。从主要活动,我开始一个新的活动,我想触发 caller.asyncResponse 在这个新的活动,主要活动同时使用同一个异步任务类。如果有两个以上的活动,我想触发 caller.asyncResponse 中的所有活动。是否有可能做到这一点?

When a message received I call caller.asyncResponse(message) and it runs in given activity. From main activity I start a new activity and I want to trigger caller.asyncResponse in this new activity and main activity simultaneously using the same async task class. If there are more then two activities I want to trigger that caller.asyncResponse in all activities. Is it possible to do this?

推荐答案

如果需要将其收到的所有活动的,那么你很可能要使用广播,而不是回调。

If it needs to be received in all Activity's then you'd probably want to use a broadcast instead of a callback.

Android电子文档的广播接收器

Android docs for BroadcastReceiver

既然你只想要你的应用程序内的广播,你也想用的 LocalBroadcastManager

And since you only want broadcasts within your app you'll also want to use it with the LocalBroadcastManager

本教程介绍你需要为这个一切。


一个BroadcastReceiver添加到每个活动像这样

Sample Add a BroadcastReceiver to each Activity like so

BroadcastReceiver rec = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //all events will be received here
            //get message
            String message = intent.getStringExtra("message");
        }
    };
    LocalBroadcastManager.getInstance(this).registerReceiver(rec, new IntentFilter("event"));

要发送广播。
相反caller.asyncResponse(消息)的;

To send a Broadcast. Instead of caller.asyncResponse(message);

  Intent intent = new Intent("event");
  // add data
  intent.putExtra("message", message);
  LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

这篇关于与多个来电Android的共同任务异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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