0

Androidデバイスのデフォルトのホーム画面として、独自のアプリケーションのホーム画面があります。アプリケーションにステータス バーがあり、そのステータス バーで Imageview(onclick) を使用して Bluetooth 接続ステータスを管理しています。

ステータスバーの Imageview をクリックすると、Bluetooth のオン/オフが切り替わります。ブルートゥースの状態に合わせてイメージビューの背景を変えています。

Bluetooth がオンの場合 - Blueimage

Bluetooth がオフの場合 - grayImage

だから私の質問は - ブルートゥース設定ページでブルートゥースを有効/無効にし、戻るボタンを押してホーム画面に移動すると、ステータスバーの Imageview (背景画像) が自動的に変更されるはずです。

戻るボタンを押して(onbackpressedメソッドをオーバーライドして)imageviewの画像を更新しようとしましたが、結果はありませんでした。

Bluetooth設定ページでBluetoothを有効/無効にするとすぐにBluetoothのステータスを読み取り/変数に保存できる同様のAPIはありますか?それに従って、ステータスバーのイメージビューのバックラウンドが自動的に変更されますか?

どんな助けでも大歓迎です、ありがとう

4

2 に答える 2

0

Bluetooth ステータスをチェックするコードを 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

アクティビティ クラスで Bluetooth がオンまたはオフになったときに受信するには、ブロードキャスト レシーバーをコードに追加する必要があります。ご参考までに :

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

}

//Bluetooth ブロードキャストをリッスンする 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 に答える