我實現了檢查wifi狀態連接與否的代碼。我做了一個文本視圖動態更新textview數據Android Java
TextView WifiTv = (TextView) findViewById(R.id.wifistate);
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiCheck = connectionManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiCheck.isConnected()) {
// Do whatever here
WifiTv.setText("WiFi is Connected");
WifiTv.notify();
} else {
WifiTv.setText("WiFi is not Connected");
}
它的工作原理,但它不會動態更新,因此,如果我關閉wifi我必須刷新活動TextView的改變(不綁定數據)。如何讓textview更新不刷新? Thx
編輯:我有這個代碼在一個單獨的類與吐司很好地工作。我想要做的只是在textview中顯示。如何將textview變量連接到這個沒有xml的類?
public void onReceive(Context context, Intent intent) {
NetworkInfo info =
intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info != null && info.isConnected()) {
// Do your work.
// e.g. To check the Network Name or other info:
WifiManager wifiManager = (WifiManager)
context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
Toast.makeText(context, "Connected to: " + ssid,
Toast.LENGTH_SHORT).show();
}
創建這些當你調用這個代碼片斷? – Ahmar