保持整个活动在Android蓝牙连接 [英] Keeping a Bluetooth connection across activities on Android

查看:707
本文介绍了保持整个活动在Android蓝牙连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/6872970/android-how-to-pass-a-bluetooth-connection-to-another-activity">Android:如何通过蓝牙连接到另一个活动?

我有一个应用程序中,我会通过蓝牙连接进行传输数据。

I have an application in which I am going to be transferring data via a Bluetooth connection.

在我的概念验证应用程序,我能够把蓝牙通信在不同的线程,并让我所有的其他工作(使用ViewFlipper)相同的活动。

In my proof of concept app, I was able to put the Bluetooth communication in a different thread and keep all of my other work on the same activity (using ViewFlipper).

不过,我的下一个迭代,我将需要使用几个不同的屏幕(活动)。该流程会去是这样的:

However, for my next iteration, I will need to use several different "screens" (activities). The flow would go something like this:

我最初的活动将连接到蓝牙设备我会越来越从数据。

My initial activity will connect up to the Bluetooth device I will be getting the data from.

一项活动掀起的主的活动将是获取数据的列表,从蓝牙设备,并在列表视图中显示它。然后我单击一个项目我将需要获得对数据的更详细的视图(其被示出在另一活性,但也有访问的BT连接)。

One activity off of the "main" activity will be to get a list of data from the BT device and display it in a list view. Then as I click on an item I will need to get a more detailed view of the data (which is shown in yet ANOTHER activity but also has to access the BT connection).

将有更多的,具体的活动掀起了主要的活动。

There will be additional, specific activities off of the "main" activity.

所以我的问题是怎么管理的最好的方法,我在那里启动了蓝牙连接一次,就不必再启动它为每一项活动?

So my question is how is the best way to manage that, where I initiate the Bluetooth connection ONCE and don't have to initiate it again for each activity?

推荐答案

正如亚历克斯说,存储控制器的连接作为一个自定义的应用程序类。

As Alex said store a controller for the connection as a member of a subclass of a custom application class.

您不得不做出这样的扩展了Android应用类自己的应用程序类,并在你的清单中注册此类。现在,你可以用 getApplication 和应用它转换为子类。现在,您可以访问您的应用程序类的成员变量。

You have to make your own application class that extends the android application class and register this class in your manifest. Now you can get the Application with getApplication and cast it to your subclass. Now you can access the member variables of your Application class.

其他一些可能性在机器人单证的。

Some other possibilities are described in the android documentations.

我就不会在你的应用程序类使用静态字段。世界上只有一个应用程序类不管怎样,你可以肯定,当你的应用程序正在运行的整个应用程序的对象不会被破坏。 Android的书籍有些作者指出,这不是一定的静态变量。

I would not use a static field in your application class. There is only one application class anyway and you can be sure that the whole application object won't be destroyed while your app is running. Some authors of Android books state that this is not that certain for static variables.

这篇关于保持整个活动在Android蓝牙连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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