Is that possible that m开发者_如何学编程y android app receive a broadcast while my network provider changed? thanks in advance.
Yes.It is posible to find this.
see the code
public class NetworkStateReceiver extends BroadcastReceiver {
public static final String TAG = "NetworkReceiver";
@Override
public void onReceive(Context context, Intent intent) {
boolean isNetworkDown = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); // 2
if (isNetworkDown) {
Log.d(TAG, "onReceive: NOT connected, stopping UpdaterService");
}
else
{
Log.d(TAG, "onReceive: connected, starting UpdaterService");
}
}
In the Manifes file add this code
<receiver android:name="NetworkStateReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
精彩评论