2012-09-03 117 views
0

我有我自己的應用程序主屏幕作爲我的android設備的默認主屏幕。我的應用程序中有一個狀態欄,我正在用狀態欄中的Imageview(onclick)管理藍牙連接狀態。狀態欄上的藍牙狀態

如果我點擊狀態欄中的ImageView,我打開/關閉藍牙。根據藍牙的狀態我正在改變imageview的背景。

如果藍牙ON - Blueimage

如果藍牙功能已關閉 - greyImage

所以我的問題是 - 如果我啓用/禁用bluetoth設置頁面中的藍牙,然後按返回鍵和導航到我家屏幕上,然後我的狀態欄中的Imageview(背景圖像)應該會自動更改。

我試了很多更新imageview上的圖像後退按鈕按下(通過覆蓋onbackpressed方法),但沒有結果。

當我們在藍牙設置頁面啓用/禁用藍牙後,根據狀態欄中的圖像視圖背景會自動更改,是否有任何類似的API可以用來讀取/存儲藍牙狀態的變量?

任何幫助是極大的讚賞,感謝

回答

0

你可以把在onRestart()方法用於藍牙狀態檢查

這樣的代碼:

public class yourClass { 

public void onCreate(Bundle savedInstanceState) { 

// your code 
} 

public void onRestart() { 

// put the checked code for the Bluetooth status here 
} 
+0

感謝您的回覆,我試了一下,但後面按下Imageview沒有變(我試着在設置頁面中啓用藍牙,並按回buton) – Randroid

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); 

}

//偵聽藍牙廣播

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 
     } 

一旦接收狀態的廣播接收器做你的功能..做!

+0

謝謝,但我想更新狀態欄上的Imageview按一旦啓用/禁用藍牙設置頁面中的bluettoth,我的設備的後退按鈕。 – Randroid

+0

你爲什麼要這樣做?並記住,而重寫onBackPressed()你必須刪除super.onBackPressed()否則你的活動將被破壞! –

+0

在我的問題中,我提到了上面所述的相同,我有我自己的應用程序設置爲我的設備的默認主屏幕,現在如果我啓用藍牙在藍牙設置頁面和按下後退按鈕應該改變ImageView在狀態酒吧(我爲Imageview背景使用2個.png文件,一個用於ON,一個用於OFF) – Randroid