我试图创建一个非常简单的应用程序,只需要一个按钮和textview,其中texview显示来自wifi连接的RSSI值。
到目前为止我制作了这段代码:
但是,当我单击按钮时,textview从0(默认值)更改为100.我认为级别值应该等于RSSI,但它不起作用。 有人可以解释一下我做错了什么吗?非常感谢!
public class MainActivity extends AppCompatActivity { private TextView level; WifiInfo wifiInfo; WifiManager wifiManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); level = (TextView) findViewById(R.id.levelText); } int linkSpeed = 100; //My default textview is O public void onReceive(Context context, Intent intent) { wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); linkSpeed = wifiManager.getConnectionInfo().getRssi(); } public void clickButton(View view) { level.setText(""+linkSpeed); }
public class MainActivity extends AppCompatActivity { private TextView level; WifiInfo wifiInfo; WifiManager wifiManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); level = (TextView) findViewById(R.id.levelText); } int linkSpeed = 100; //My default textview is O public void onReceive(Context context, Intent intent) { wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); linkSpeed = wifiManager.getConnectionInfo().getRssi(); } public void clickButton(View view) { level.setText(""+linkSpeed); }
答案 0 :(得分:0)
我解决了我的问题,我真的没有很好地理解这些功能(我认为onReceive被称为onCreate)。现在,这是我感兴趣的人的代码:
public class WiFiActivity extends AppCompatActivity {
// Variables
private static final String TAG = WiFiActivity.class.getSimpleName();
private int value; // RSSI value
private TextView textRSSI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wi_fi);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Log.i(TAG, "Test");
textRSSI = (TextView) findViewById(R.id.TextRSSI);
//initializeWiFiListener();
}
public void myClickHandler(View view) {
initializeWiFiListener();
}
private void initializeWiFiListener(){
Log.i(TAG, "executing initializeWiFiListener");
String connectivity_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager)getSystemService(connectivity_context);
if(!wifi.isWifiEnabled()){
if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
wifi.setWifiEnabled(true);
}
}
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
WifiInfo info = wifi.getConnectionInfo();
value = info.getRssi();
Log.i(TAG, Integer.toString(value));
if(value <= -30 && value > -67) // Amazing
textRSSI.setTextColor(Color.GREEN);
else if(value <= -67 && value > -70) // Very good
textRSSI.setTextColor(Color.GREEN);
else if(value <= -70 && value > -80) // Okay
textRSSI.setTextColor(Color.YELLOW);
else if(value <= -80 && value > -90) // Not good
textRSSI.setTextColor(Color.RED);
else if(value <= -90) // Unusable
textRSSI.setTextColor(Color.RED);
textRSSI.setText(""+value);
}
}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
}