我想要一个应用程序在使用toast消息连接到wifi时显示WIFI SSID,我也希望这种情况发生在已杀死状态,我该怎么办呢。 谢谢!提前回复 主要活动
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
TextView textConnected, textSsid, textBssid, textMac, textRssi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textConnected = (TextView) findViewById(R.id.Connected);
textSsid = (TextView) findViewById(R.id.Ssid);
textBssid = (TextView) findViewById(R.id.Bssid);
textMac = (TextView) findViewById(R.id.Mac);
textRssi = (TextView) findViewById(R.id.Rssi);
/*DisplayWifiState();*/
this.registerReceiver(this.myWifiReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver myWifiReceiver
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null
&& !(connectionInfo.getSSID().equals(""))) {
String ssid = connectionInfo.getSSID();
android.widget.Toast toast = android.widget.Toast.makeText(
context, "Wifi Connected to " + ssid, android.widget.Toast.LENGTH_LONG);
toast.show();
} else {
android.widget.Toast toast = android.widget.Toast.makeText(
context, "Connect to WiFi first", android.widget.Toast.LENGTH_LONG);
toast.show();
}
}
}
};
/*private void DisplayWifiState(){
ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
textMac.setText(myWifiInfo.getMacAddress());
if (myNetworkInfo.isConnected()){
textConnected.setText("--- CONNECTED ---");
textSsid.setText(myWifiInfo.getSSID());
textBssid.setText(myWifiInfo.getBSSID());
textRssi.setText(String.valueOf(myWifiInfo.getRssi()));
}
else{
textConnected.setText("--- DIS-CONNECTED! ---");
textSsid.setText("---");
textBssid.setText("---");
textRssi.setText("---");
}
}*/
}
答案 0 :(得分:-1)
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
boolean found = false;
for (WifiConfiguration wifiConfig : configuredNetworks) {
if (wifiConfig.SSID != null && (wifiConfig.SSID.toString().replaceAll("\"", "")).contains(YOUR_SSID)) {
//YOUR CODE GOES HERE
break;
}
}
这对我有用