如何让wifi状态改变只广播一次
how to get wifi state change Broadcast only once
在我的应用程序中,我只想广播一次 wifi 状态更改。但是,当wifi变了,我收到了很多次广播,怎么办?
public class WifiChangedReceiver extends BroadcastReceiver {
public static String TAG = "WifiChangedReceiver";
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isConnected = wifi != null && wifi.isConnected();
if (isConnected) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
String wifi_ssid = wifiManager.getConnectionInfo().getSSID();
Log.d(TAG, "wifi connected==="+wifi_ssid);
} else {
Log.d(TAG, "wifi disconnected");
}
}
public class MainActivity extends Activity {
private WifiChangedReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(mReceiver==null){
mReceiver = new WifiChangedReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy() {
if(mReceiver!=null){
unregisterReceiver(mReceiver);
}
super.onDestroy();
}
LogCat: wifi Camus to Tutu
enter image description here
LogCat: wifi Tutu 到 Camus
enter image description here
首先在共享首选项中存储一个值 "off" 并在 unregistering.so 期间更改它的值,它不会再次注册,所以你不会再次收到广播.
pf=preferenceManager.getdefaultsharedpreferences();
pf.edit().getString("status")
if(pf.getString(状态).等于("off"))
{
注册接收器
}
然后在注销期间
{
pf.edit().putString("status","ON")
取消注册接收器
}
我通过google解决了这个问题,虽然没有立即反应,但对我的项目很有用。我发布了代码,可能对其他人有用。
public class WifiChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
NetworkInfo netInfo = (NetworkInfo) b
.get(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = netInfo.getState();
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if ((state == NetworkInfo.State.CONNECTED) && (activeNetInfo != null)
&& (activeNetInfo.getType() == netInfo.getType())) {
Log.d(TAG, "wifi connected");
}
}
}
在我的应用程序中,我只想广播一次 wifi 状态更改。但是,当wifi变了,我收到了很多次广播,怎么办?
public class WifiChangedReceiver extends BroadcastReceiver {
public static String TAG = "WifiChangedReceiver";
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isConnected = wifi != null && wifi.isConnected();
if (isConnected) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
String wifi_ssid = wifiManager.getConnectionInfo().getSSID();
Log.d(TAG, "wifi connected==="+wifi_ssid);
} else {
Log.d(TAG, "wifi disconnected");
}
}
public class MainActivity extends Activity {
private WifiChangedReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(mReceiver==null){
mReceiver = new WifiChangedReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy() {
if(mReceiver!=null){
unregisterReceiver(mReceiver);
}
super.onDestroy();
}
LogCat: wifi Camus to Tutu enter image description here
LogCat: wifi Tutu 到 Camus enter image description here
首先在共享首选项中存储一个值 "off" 并在 unregistering.so 期间更改它的值,它不会再次注册,所以你不会再次收到广播.
pf=preferenceManager.getdefaultsharedpreferences();
pf.edit().getString("status")
if(pf.getString(状态).等于("off")) {
注册接收器
}
然后在注销期间
{
pf.edit().putString("status","ON")
取消注册接收器
}
我通过google解决了这个问题,虽然没有立即反应,但对我的项目很有用。我发布了代码,可能对其他人有用。
public class WifiChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
NetworkInfo netInfo = (NetworkInfo) b
.get(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = netInfo.getState();
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if ((state == NetworkInfo.State.CONNECTED) && (activeNetInfo != null)
&& (activeNetInfo.getType() == netInfo.getType())) {
Log.d(TAG, "wifi connected");
}
}
}