安卓:活动之间共享数据 [英] Android: sharing data between activities

查看:135
本文介绍了安卓:活动之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程一个Android应用程序,执行以下操作(简言之):

I am programming an android app that does the following (in a nutshell):

  1. 在创建监听到的数据通过蓝牙流活动
  2. 在解析流中的数据
  3. (计划)显示PTED数据含义的相互$ P $,像图/文

有关#3,我打算创建一个坐在蓝牙流活动之上的新的活动 - 毕竟,蓝牙事项应在后初始设置背景

For #3, I am planning to create a new activity that sits on top of the Bluetooth Streaming activity -- afterall, the bluetooth matters should be in the background after initial set up.

不过,我有B​​T的活动和展示活动之间发送的数据的问题。

However, I am having issues sending the data between the BT activity and the display activity.

我用Google搜索,研究和最好的我能想出使用意图这一点,但它不工作。

I have googled, researched and the best I can come up with is using Intents for this, but its not working.

下面是一个例子。假设数据进来,我跨preT的数据涉及身体的运动:

Here is an example. Say data is coming in and I interpret the data involve body movement:

private BodyMovementPositionStream() {
    if (getActivity() != null && getContext() != null && iDebug == null) {
        iDebug = new Intent(getActivity(), AGDebug.class);
        iDebug.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        new Thread(new Runnable() {
            public void run() {
                if (theApplication.isD()) {
                    Log.d(theApplication.getTAG(), "Creating Intent for Position Debuging...");
                    getContext().registerReceiver(new AGDReceiver(), new IntentFilter("com.ekscsy.sleepstudy.debug.AGDebug"));
                    getContext().startActivity(iDebug);
                }
            }
        }).start();
    }
}

因此​​,基本上,在剪断-它,我开始活动AGDebug,新的任务添加了标记,然后我注册一个新线程的接收器。这工作,我看到它在我的手机和 Log.d()信息显示出来。

后来在我的code,我提取数据:

Later on in my code, I extract the data:

    private static final int fSize = 4;
    public void unpack(byte[] data) {
        int i = -fSize;
        accx = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        accy = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        accz = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        gyrox = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        gyroy = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        gyroz = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        temp = Streams.byteArr2ByteBuff(data, i += fSize, fSize).order(null).getFloat();
        Intent intent = new Intent("com.ekscsy.sleepstudy.debug.AGDReceiver");
        intent.putExtra("com.ekscsy.sleepstudy.debug.AGDebug.accxF", accx);
        getContext().sendBroadcast(intent);
    }

在提取结束时,我提出一个新的目标, .putExtra()包+有一些数据的浮动的名称。我做的 .sendBroadcast()但是......没有任何反应。我有一个永远不会被调用断点:

At the end of the extraction, I make a new intent, .putExtra() the name of the package + a float with some data. I do the .sendBroadcast() but... nothing happens. I have a breakpoint that never gets called:

public class AGDReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        assert(true); //breakpoint here
    }

}

很显然,我缺少的步骤在这里,但我似乎无法找到活动之间共享数据的任何有用信息。

Obviously, I am missing steps here, but I can't seem to find any good information on sharing data between activities.

其实,让我重新迭代,这里的主要目标是活动之间的共享数据。数据显示,作为花车,整数,等我假设这样的Android的方式是通过意图和广播,但如果有更好的方法,我所有的耳朵。

In fact, let me re-iterate, the main goal here is the share data between activities. Data, being floats, ints, etc. I am assuming the "Android" way of doing this is through intents and broadcast, but if there is a better method, I am all ears.

我期待以与API 8这个应用程序兼容。

I am looking to make this app compatible with API 8.

编辑:仅供参考,我改变了code采取AGDebug活动开始了自己的线程。它看起来更象现在这样:

FYI, I changed the code to take the start of the AGDebug activity out of its own thread. It looks more like this now:

    if (getActivity() != null && getContext() != null && iDebug == null) {
        iDebug = new Intent(getActivity(), AGDebug.class);
        iDebug.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Log.d(theApplication.getTAG(), "Creating Intent for Position Debuging...");
        getContext().registerReceiver(new AGDReceiver(), new IntentFilter("com.ekscsy.sleepstudy.debug.AGDebug"));
        getContext().startActivity(iDebug);
    }

仍然没有虽然改变任何东西。我想也许有不同的运行手动创建的线程之间的活动的问题。

Still didn't change anything though. I thought maybe there was an issue of running the activity between different manually created threads.

主要更新/新的问题:

我发现这个指南 http://www.sohailaziz.com/ 2012/04 / localbroadcastmanager-内application.html ,并能够成功实施使与LocalBroadcastManager广播工作的战略。但是...这是非常奇怪的......我发现了 onReceieve()方法被有时候还是不叫。 IOW:当我运行的应用程序,要么将调用每一个我说 lbm.sendBroadcast(意向),否则它将不会被调用在所有时间

I found this guide http://www.sohailaziz.com/2012/04/localbroadcastmanager-intra-application.html and was able to successfully implement its strategy of making the broadcaster work with the LocalBroadcastManager. However... and this is very weird... I am finding the onReceieve() method gets called sometimes or not. IOW: when I run the app, either it will get called each time I say lbm.sendBroadcast(intent) or it won't get called at all.

很令人费解。

最后编辑: 好吧,我得到它的工作。有确保每个意图正确,点名叫到正确的位置的微妙的艺术。一旦这样做,它似乎是一致的。

Final Okay I got it to work. There is a delicate art of making sure each intent is named correctly and points to the right spot. Once done, it seems to be consistent.

推荐答案

您注册的广播接收器是这样的:

You register the broadcast receiver like this:

getContext().registerReceiver(new AGDReceiver(),
    new IntentFilter("com.ekscsy.sleepstudy.debug.AGDebug"));

但广播这个通知:

but you broadcast this Intent:

Intent intent = new Intent("com.ekscsy.sleepstudy.debug.AGDReceiver");

在意图的操作是不一样的。如果你定义,你可以节省自己这个麻烦不断的对于这一点,即:

The actions in the Intents are not the same. You could have saved yourself this trouble if you defined a constant for this, ie:

public static final String MY_INTENT_ACTION = "com.ekscsy.sleepstudy.debug.AGDReceiver";

,然后使用该常量,当你创建广播意图,当你创建意图过滤器用于注册的广播接收器。

and then used that constant when you create the broadcast intent and when you create the intent filter for registering the broadcast receiver.

这篇关于安卓:活动之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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