开发者

is there a broadcast if network provider changes in Android?

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜