Java 在Android中,如何将BluetoothAdapter从一个活动传递到另一个活动?
我有一个名为MainActivity.java的类/活动,我使用Java 在Android中,如何将BluetoothAdapter从一个活动传递到另一个活动?,java,android,exception-handling,bluetooth,android-bluetooth,Java,Android,Exception Handling,Bluetooth,Android Bluetooth,我有一个名为MainActivity.java的类/活动,我使用BluetoothAdapter BA检查蓝牙是否开启。我有另一个名为Search.java的类,它搜索附近的蓝牙设备,但它必须使用MainActivity.java中创建的相同BluetoothAdapter进行搜索。我该怎么做 我在MainAactivity.java中有一个按钮,它的onClick必须在另一个类中定义,我该怎么做 如果这些问题很愚蠢的话,我很抱歉,但我是Android新手&对Java的使用经验很少 谢谢你的时间
BluetoothAdapter BA
检查蓝牙是否开启。我有另一个名为Search.java的类,它搜索附近的蓝牙设备,但它必须使用MainActivity.java中创建的相同BluetoothAdapter进行搜索。我该怎么做
我在MainAactivity.java中有一个按钮,它的onClick必须在另一个类中定义,我该怎么做
如果这些问题很愚蠢的话,我很抱歉,但我是Android新手&对Java的使用经验很少
谢谢你的时间
我有一个名为MainActivity.java的类/活动,我使用蓝牙适配器BA检查蓝牙是否开启。我有另一个名为Search.java的类,它搜索附近的蓝牙设备,但它必须使用MainActivity.java中创建的相同BluetoothAdapter进行搜索。我该怎么做
(编辑:请参见下面的我的编辑,您不需要在您的案例中实现此功能)您可以实现存储和获取BluetoothAdapter,这样您就可以在项目中的任何位置访问它。应该是这样的:
import android.bluetooth.BluetoothAdapter;
public class BluetoothSingleton {
private static BluetoothSingleton mInstance = new BluetoothSingleton();
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSingleton(){
// Private constructor to avoid new instances
}
public static BluetoothSingleton getInstance(){
return mInstance;
}
public void setBluetoothAdapter(BluetoothAdapter adapter){
mBluetoothAdapter = adapter;
}
public BluetoothAdapter getBluetoothAdapter(){
return mBluetoothAdapter;
}
}
而且我在MainActivity.java中有一个按钮,它的onClick必须在另一个类中定义,我该怎么做
您只需创建一个方法来设置该按钮侦听器,并通过如下方式传递:
import android.bluetooth.BluetoothAdapter;
public class BluetoothSingleton {
private static BluetoothSingleton mInstance = new BluetoothSingleton();
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSingleton(){
// Private constructor to avoid new instances
}
public static BluetoothSingleton getInstance(){
return mInstance;
}
public void setBluetoothAdapter(BluetoothAdapter adapter){
mBluetoothAdapter = adapter;
}
public BluetoothAdapter getBluetoothAdapter(){
return mBluetoothAdapter;
}
}
AnotherClass.java:
...
public void setButtonListener(View.OnClickListener listener){
mButton.setOnClickListener(listener);
}
...
然后您可以在MainActivity.java中进行设置:
...
AnotherClass another = new AnotherClass();
another.setButtonListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do what you want here
}
});
...
编辑1:
实际上,你可以打电话:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
您将得到相同的适配器
我有一个名为MainActivity.java的类/活动,我使用蓝牙适配器BA检查蓝牙是否开启。我有另一个名为Search.java的类,它搜索附近的蓝牙设备,但它必须使用MainActivity.java中创建的相同BluetoothAdapter进行搜索。我该怎么做
(编辑:请参见下面的我的编辑,您不需要在您的案例中实现此功能)您可以实现存储和获取BluetoothAdapter,这样您就可以在项目中的任何位置访问它。应该是这样的:
import android.bluetooth.BluetoothAdapter;
public class BluetoothSingleton {
private static BluetoothSingleton mInstance = new BluetoothSingleton();
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSingleton(){
// Private constructor to avoid new instances
}
public static BluetoothSingleton getInstance(){
return mInstance;
}
public void setBluetoothAdapter(BluetoothAdapter adapter){
mBluetoothAdapter = adapter;
}
public BluetoothAdapter getBluetoothAdapter(){
return mBluetoothAdapter;
}
}
而且我在MainActivity.java中有一个按钮,它的onClick必须在另一个类中定义,我该怎么做
您只需创建一个方法来设置该按钮侦听器,并通过如下方式传递:
import android.bluetooth.BluetoothAdapter;
public class BluetoothSingleton {
private static BluetoothSingleton mInstance = new BluetoothSingleton();
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSingleton(){
// Private constructor to avoid new instances
}
public static BluetoothSingleton getInstance(){
return mInstance;
}
public void setBluetoothAdapter(BluetoothAdapter adapter){
mBluetoothAdapter = adapter;
}
public BluetoothAdapter getBluetoothAdapter(){
return mBluetoothAdapter;
}
}
AnotherClass.java:
...
public void setButtonListener(View.OnClickListener listener){
mButton.setOnClickListener(listener);
}
...
然后您可以在MainActivity.java中进行设置:
...
AnotherClass another = new AnotherClass();
another.setButtonListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do what you want here
}
});
...
编辑1:
实际上,你可以打电话:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
您将获得相同的适配器。为什么不能从上下文对象执行此操作?所有连接逻辑都必须在服务中完成,因此您可以绑定所有活动,并根据需要进行绑定……@EagleEye请解释如何执行此操作。我是Android的新手。为什么你不能从上下文对象进行操作呢?所有的连接逻辑都必须在一个服务中完成,这样你就可以绑定所有的活动和你想要的任何内容……@EagleEye请解释一下如何做到这一点。我对Android是全新的。