如何将数据从非活动类传递到android中的活动? [英] How to pass data from a non-activity class to an activity in android?

查看:84
本文介绍了如何将数据从非活动类传递到android中的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的应用程序,并且正在使用socket.io与Web服务器进行通信.我有处理这些不是活动的Java文件,而是在后台运行.因此,要启动它们,将在一个活动中将其实例化,然后套接字将运行.现在,Web服务器触发了我在回调中处理的消息,并将数据传递给活动.现在,我正在寻找将数据从正在运行的非活动类传递到当前正在运行的活动的最佳方法.

I'm building my app and I'm using socket.io to communicate with a webserver. I have java files that take care of that which are not activities and run in the background. So to start them, it will be instantiated in an activity and the sockets will run. Now the webserver triggers messages which I handle in the callback and pass the data to an activity. Now I'm looking for the best way to pass data from a running non-activity class to an activity currently running.

我之前所做的是使用非活动类创建的意图,该意图将数据发送到当前正在运行的活动.活动必须更改为单实例或单顶层,以避免活动继续自行重启.问题是,从我的主要活动性来看,我不能使用单顶启动模式,因为在某些情况下,我需要活动性对其自身进行刷新.

What I did before was using an intent created by the non-activity class which sent the data to the currently running activity. The actvity had to be changed to singleinstance or singletop to avoid the activity from keeping on restarting on itself. The problem is that form my main actvity I can't use single top launchmode because I need the actvity to refresh on itself in some instances.

因此我无法使套接字在主要活动上正常工作,因为只有将主要活动更改为singletop才能接收数据,这对应用程序而言并不理想.我首先尝试用非活动性类调用的主动性来命名一个函数,但似乎这是非法的,所以我很困在这里.

So I can't get the sockets properly working on the main activity because I can only recieve data if I change my main activity to singletop which is not ideal for the app. I had first tried nameing a function in the acivity that would be called by the non-activity class but it seems that, that's illegal, so I'm pretty stuck here.

@Override
    public void on(String event, IOAcknowledge ack, Object... data) {

        System.out.println("Server may have triggered event '" + event + "'");

        if (event.equals("message")) {
        System.out.println(data[2]);

        Intent newIntent = new Intent(Chat.mContent, Chat.class);
        Bundle extras=new Bundle();
        extras.putString("name", (String)data[1]);
        extras.putString("MSG", (String)data[2]);
        newIntent.putExtras(extras);


        Chat.mContent.startActivity(newIntent);

        }
        if (event.equals("ready")) {
            System.out.println("Received the ready callback");
            try{
            Intent newIntent = new Intent(TabExercise.mContext, TabExercise.class);
            newIntent.putExtra("ready", event);
            TabExercise.mContext.startActivity(newIntent);
            }catch(Exception e){
                System.out.println("Caught an error");
            }


        }
    }

这是非活动类中发送数据的方法.

This is the method in the non-activity class where the data is sent.

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras=intent.getExtras();

if (intent.hasExtra("ready")) {
    // Retrieve the message and call getMsg(String)
   connect.starter(SaveSharedPreference.getName(TabExercise.this), SaveSharedPreference.getUserId(TabExercise.this));

}

}

这是在mainactivity中捕获意图的地方.不理想.

This is the where the intent is caught in the mainactivity. Not ideal.

推荐答案

我认为您可能会发现有用的是使用 Singletons 或使用非UI的 Fragment .如果您尚未在用户界面中使用Fragments,则绝对应该对其进行调查,因为它是现代android应用程序的法律标准.此外,Android支持库将Fragments一直返回到Android 1.6,这是向后兼容的理想选择.

I think what you may find helpful is to use Singletons or perhaps a non-UI Fragment. If you aren't using Fragments already for your UI, you should definitely look into it because it is the de jure standard for modern android apps. What's more, the Android Support Library gives Fragments all the way back to Android 1.6 which is ideal for backwards compat.

您同样可以在线程之间使用 Handlers 传递数据和消息.这些基本上提供了一个管道,您可以将消息 post 发送到和 handleMessage .您可能会在 Application onCreate 中启动网络工作,并且可能将该线程存储为 singleton .然后,您将对该UI的引用 Handler 传递给该线程,然后可以通过发布 Runnables 直接与您的UI通信.

You could equally pass data and messages around using Handlers between threads. These basically provide a pipeline which you can post messages to and handleMessage. You would probably start your network stuff in your Application onCreate and perhaps store the thread as a singleton. You would then pass a reference to your UI Handler to this thread which can then communicate directly to your UI by posting Runnables.

在这里可以找到一个有趣的答案,更好":

An interesting answer for which is 'better' can be found here:

非UI片段vs Singleton

这篇关于如何将数据从非活动类传递到android中的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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