我有我的其中BT建立连接的第一项活动。有psented给用户的选项$ P $,并根据他们的选择,一个不同的活动将加载
I have my first Activity in which the BT connection is established. There is an option presented to the user and, based on their selection, a different Activity will load.
现在的问题是,这两个活动都需要一个BT连接,我没有看到这一点摧毁一个连接只是再拍。
The problem is, both activities need a BT connection and I don't see the point in destroying one connection just to make another.
有没有办法,我可以通过活动之间的联系的方式?
Is there a way that I could pass the connection between Activities?
有没有人有一些例子,我或者一个链接?
Does anyone have some example for me or perhaps a link?
我已经试过类MyApplication的扩展应用
,但我不能使用:
I've tried "class MyApplication extends Application
", but then I can't use:
super.onCreate(savedInstanceState);
setContentView(R.layout.blablabla);
这可能是pretty的愚蠢的问题,但我只在Android的+ - 。2周
This may be a pretty silly question but I've only been at Android +- 2 weeks.
您是否尝试过使用Application对象存储在对象中的蓝牙连接,并使用你的活动得到它?
Have you tried using the Application object to store the Bluetooth connection in an object and using your Activities to get it?
尝试这样的事情。 (注:我从来没有与蓝牙在Android上工作过,所以我不知道该用哪些相关的类在这种情况下,我会使用 BluetoothDevice类
,因为它似乎。基于该库文件是正确的类)
Try something like this. (Note: I have never worked with Bluetooth on Android, so I don't know which relevant classes to use. In this case, I'll use BluetoothDevice
, since it seems to be the right class based on the library documentation)
public class MyApplication extends Application {
BluetoothDevice device;
...
public synchronized BluetoothDevice getBtConnection() {
if (device == null) {
// construct a BluetoothDevice object and put it into variable device
}
return device;
}
}
这样的话,你的第一个活动只是要做到这一点:
That way, your first activity just has to do this:
public class FirstActivity extends Activity {
private BluetoothDevice device;
...
@Override
protected void onCreate(Bundle b) {
super(b);
...
device = ((MyApplication) getApplication()).getBtDevice();
...
}
...
}
然后,任何时候你的其他活动需要用到这方面,他们只需要调用 getBtDevice()
,因为 FirstActivity
已实例吧。