我试图找到一种方法来监视 WiFi 接入点的状态变化,但我发现该常量WIFI_AP_STATE_CHANGED_ACTION
被隐藏了。有没有其他方法可以监控这些状态变化?
这是某人的解决方案,但恐怕并非所有手机都始终支持它。谁能给我一些建议?很感谢。
我试图找到一种方法来监视 WiFi 接入点的状态变化,但我发现该常量WIFI_AP_STATE_CHANGED_ACTION
被隐藏了。有没有其他方法可以监控这些状态变化?
这是某人的解决方案,但恐怕并非所有手机都始终支持它。谁能给我一些建议?很感谢。
请检查一下。
public class BroadCastSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.registerReceiver(this.mConnReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo.isConnected()){
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
}
}
};
}