Android 多次触发广播连接更改
在BroadcastReceiver中,我使用此代码检测设备何时连接到互联网,何时连接可用-WiFi/数据:Android 多次触发广播连接更改,android,wifi,broadcast,connectivity,receiver,Android,Wifi,Broadcast,Connectivity,Receiver,在BroadcastReceiver中,我使用此代码检测设备何时连接到互联网,何时连接可用-WiFi/数据: if(arg1.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager connMgr = (ConnectivityManager) arg0.getSystemService(Context.CONNECTIVITY_SERVICE); N
if(arg1.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connMgr = (ConnectivityManager) arg0.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable()) {
Toast.makeText(Context, "Connected to internet", Toast.LENGTH_LONG).show();
}
问题是:
电话断开了
我启用WiFi或数据连接
我收到通知,但次数太多了。电话仍然连接,但我一直收到通知
为什么?
谢谢你可以试试这个:
public class MainActivity extends Activity {
private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive( final Context context, final Intent intent ) {
MainActivity.this.oldWifiConnectionState = MainActivity.this.currentWifiConnectionState;
boolean wifiConnected = false;
final ConnectivityManager connMgr = ( ConnectivityManager ) context.getSystemService( Context.CONNECTIVITY_SERVICE );
final NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if ( ( networkInfo != null ) && networkInfo.isConnected() && networkInfo.isAvailable() ) {
wifiConnected = true;
}
// or you can use
// wifiConnected = ( activeNetwork != null ) && activeNetwork.isConnectedOrConnecting();
boolean mobileDataEnabled = false; // Assume disabled
try {
final Class cmClass = Class.forName( connMgr.getClass().getName() );
final Method method = cmClass.getDeclaredMethod( "getMobileDataEnabled" );
method.setAccessible( true ); // Make the method callable
// get the setting for "mobile data"
mobileDataEnabled = ( Boolean ) method.invoke( connMgr );
} catch ( final Exception e ) {
mobileDataEnabled = false;
}
MainActivity.this.currentWifiConnectionState = ( wifiConnected || mobileDataEnabled ) ? WifiConnectionState.CONNECTED : WifiConnectionState.NOT_CONNECTED;
if ( !MainActivity.this.currentWifiConnectionState.equals( MainActivity.this.oldWifiConnectionState ) ) {
// Notifiy any handlers.
}
}
}
private enum WifiConnectionState {
CONNECTED, NOT_CONNECTED, UNKNOWN
}
private WifiConnectionState currentWifiConnectionState;
private ConnectivityBroadcastReceiver mReceiver = null;
private WifiConnectionState oldWifiConnectionState;
@Override
protected void onCreate( final Bundle savedInstanceState ) {
this.currentWifiConnectionState = WifiConnectionState.UNKNOWN;
this.mReceiver = new ConnectivityBroadcastReceiver();
super.onCreate( savedInstanceState );
}
@Override
protected void onPause() {
this.unregisterReceiver( this.mReceiver );
super.onPause();
}
@Override
protected void onResume() {
this.registerReceiver( this.mReceiver, new IntentFilter( "android.net.conn.CONNECTIVITY_CHANGE" ) );
super.onResume();
}
}
谢谢你的回答,但这和我的代码差不多,试着打开你的活动,启用Wifi&你会看到它被多次触发,比如说,这是安卓本身的一个Bug?设备连接后会多次发送广播,我尝试了所有可用的方法等。您的广播有时会收到此通知。您可以使用enum WifiConnectionState来控制此通知。抱歉,但是什么是enum WifiConnectionState?在我的代码中,我创建了一个名为WifiConnectionState的enum来控制wifi状态已连接、未连接,并在该状态发生更改时通知所有处理程序。