2013-02-17 69 views
0

我是新來的java和當前正在一個項目上工作。我正在嘗試熟悉藍牙。到目前爲止,我已經能夠打開和關閉應用程序內的藍牙。我遇到的問題是執行操作後屏幕不刷新。例如,當我打開藍牙時,它會出現,但textview中的文本不會更新,直到我將手機翻過來。當我斷開連接時也是如此。這裏是我的代碼屏幕拒絕刷新行動

package com.example.bluetooth; 

import android.os.Bundle; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 


public class MainActivity extends Activity { 


BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
Button button; 
TextView update; 
CharSequence enabled = "Bluetooth Enabled"; 
CharSequence disabled = "Bluetooth disabled"; 
CharSequence connect = "Connect"; 
CharSequence disconnect = "Disconnect"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setUp(); 
    action(); 
    } 

private void action() { 
    if (button.getText() == connect) { 
     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       btAdapter.enable(); 

      } 
     }); 
    } 

    if (button.getText() == disconnect) { 
     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       btAdapter.disable(); 

      } 
     }); 
    } 

} 

private void setUp() { 
    button = (Button) findViewById(R.id.button1); 
    update = (TextView) findViewById(R.id.textView1); 



    button = (Button) findViewById(R.id.button1); 
    if (btAdapter.isEnabled()) { 

     update.setText("Bluetooth is enabled"); 
     button.setText(disconnect); 

    } 

    else { 

     update.setText("Bluetooth is disabled"); 
     button.setText(connect); 


    } 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

任何幫助都將不勝感激。謝謝。

回答

1

兩件事情我看錯:

  1. 要調用負責更新的TextView(setIp())的方法只有一次,因爲onCreate()只是每次活動創建調用一次。自從轉入adapter on/off, is done outside the UI Thread後,您將必須製作BroadcastReceiver並偵聽BluetoothAdapter.ACTION_STATE_CHANGED,然後進行相應更新。
  2. 您在比較CharSequences和==。您應該改用Strings,然後使用.equals(),因爲這樣可以確保深度比較。只有你需要更改的besised類型聲明是在getText()之後添加.toString()。例如button.getText().toString()

編輯:一個簡單的(但雜亂的)解決方案可以使接收機動態地註冊。現在,請記住,您只需要撥打setUp()一次,就像以前一樣。像下面的代碼可能工作。

public class MainActivity extends Activity { 


    BluetoothAdapter btAdapter; 
    Button button; 
    TextView update; 
    String enabled = "Bluetooth Enabled"; 
    String disabled = "Bluetooth disabled"; 
    String connect = "Connect"; 
    String disconnect = "Disconnect"; 
    BroadcastReceiver receiver; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    setUp(); 
    action(); 
    } 

    @Override 
    public void onPause() 
    { 
    try{ 
     unregisterReceiver (receiver); 
    } 
    catch (NullPointerException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IllegalStateException ie) 
    { 
     ie.printStackTrace(); 
    } 
    super.onPause(); 
    } 

    private void action() { 
    button.setOnClickListener(new View.OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
     if (btAdapter.isEnabled()) 
      btAdapter.disable(); 
     else 
      btAdapter.enable(); 

    } 

    }); 

} 

    private void setButtonText() 
    { 
    if (btAdapter.isEnabled()) {  
     update.setText("Bluetooth is enabled"); 
     button.setText(disconnect); 
    } 

    else { 

     update.setText("Bluetooth is disabled"); 
     button.setText(connect); 
    } 
    } 
    private void setUp() { 
    button = (Button) findViewById(R.id.button1); 
    update = (TextView) findViewById(R.id.textView1); 
    setButtonText(); 

    receiver = new BroadcastReceiver(){ 

     @Override 
     public void onReceive(Context context, Intent intent) { 
     setButtonText(); 

     } 

    }; 

    IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED); 
    registerReceiver (receiver, filter); 

    } 
} 
+0

謝謝。我試着在每個onclicklisteners中再次調用'setup()',但它仍然不起作用。我也從'=='更改爲'.equals()' – 2013-02-18 00:18:40

+0

@hauwamusa啓用禁用是異步調用,因此您必須製作廣播接收器。我會盡快做一個代碼示例。 – 2013-02-18 00:24:37

+0

好的,非常感謝。我會關注你的樣品。 – 2013-02-18 00:29:54