开发者

is there a broadcast if network provider changes in Android?

开发者 https://www.devze.com 2023-03-16 06:35 出处:网络
Is that possible that m开发者_如何学编程y android app receive a broadcast while my network provider changed?

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

精彩评论

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