0

我有自己的应用程序主屏幕作为我的 android 设备的默认主屏幕。我的应用程序中有一个状态栏,我正在使用该状态栏中的 Imageview(onclick) 管理蓝牙连接状态。

如果我单击状态栏中的 Imageview,我将打开/关闭蓝牙。我正在根据蓝牙的状态更改 imageview 的背景。

如果蓝牙开启 - Blueimage

如果蓝牙关闭 - grayImage

所以我的问题是 - 如果我在蓝牙设置页面中启用/禁用蓝牙并按下后退按钮并导航到我的主屏幕,那么我的状态栏中的 Imageview(背景图像)应该会自动更改。

我尝试了很多更新后退按钮时 imageview 的图像(通过覆盖 onbackpressed 方法),但没有结果。

只要我们在蓝牙设置页面中启用/禁用蓝牙并据此状态栏图像视图背景将自动更改,是否有任何类似的 API 可以让我们在变量中读取/存储蓝牙的状态?

非常感谢任何帮助,谢谢

4

2 回答 2

0

您可以将检查蓝牙状态的代码放在 onRestart() 方法中

像这样:

public class yourClass {

 public void onCreate(Bundle savedInstanceState) {

 // your code
 }

 public void onRestart() {

 // put the checked code for the Bluetooth status here  
 } 
于 2012-09-03T06:09:02.130 回答
0

您需要在代码中添加广播接收器,以便在您的活动类中打开或关闭蓝牙后接收。供你参考 :

public void onCreate() {

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);
    this.registerReceiver(mReceiver, filter3);

}

//监听蓝牙广播的BroadcastReceiver

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        ... //Device found
        }
        else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
        ... //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
         ... //Done searching
        }
        else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
        ... //Device is about to disconnect
        }
        else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
        ... //Device has disconnected
        }   

一旦您收到状态,请执行您的功能..完成!

于 2012-09-03T06:18:54.120 回答