这是 list 部分
<receiver
android:name="my.com.app.ConnectivityModifiedReceiver"
android:label="NetworkConnection" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
这是java代码
public class my.com.app.ConnectivityModifiedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.sendBroadcast(new Intent("ConnectivityModified"));
}
}
ConnectivityModifiedReceiver 将根据网络连接变化发送 Intent 。在我的例子中,VPN 连接和断开连接。
我在 Lollipop 中获得了 Intent ,但在 JellyBean 中却没有。
请帮忙
最佳答案
到目前为止我的发现
在 Lollipop 中
android.net.conn.CONNECTIVITY_CHANGE 广播仅在 VPN 连接或断开连接时触发。
因此,您可以使用以下代码 fragment 以及您对前 Lollipop 设备的逻辑。
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String KeyNI="networkInfo";
android.net.NetworkInfo bundleNetworkInfo=(android.net.NetworkInfo)bundle.get(KeyNI);
if(!bundleNetworkInfo.getTypeName().toString().equalsIgnoreCase("VPN"))
{
context.sendBroadcast(new Intent("ConnectivityModified"));
}
}
希望这对您有所帮助。
关于android.net.conn.CONNECTIVITY_CHANGE 广播接收器不会在 JellyBean 中触发以进行 VPN 连接和断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062487/