【问题标题】:android.net.conn.CONNECTIVITY_CHANGE broadcast receiver not fires in JellyBean for VPN connection and disconnectionandroid.net.conn.CONNECTIVITY_CHANGE 广播接收器不会在 JellyBean 中触发 VPN 连接和断开连接
【发布时间】:2015-07-15 17:58:12
【问题描述】:

这是清单部分

  <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 将根据网络连接变化发送意图。在我的例子中 VPN 连接和断开

我在 Lollipop 中得到了意图,但在 JellyBean 中没有。

请帮忙

【问题讨论】:

  • 基本上我想知道VPN是断开还是连接
  • 如果您有任何疑问,可以问我。我愿意为您提供帮助。
  • Shravan 您对此有什么解决方案吗?我遇到了同样的问题。

标签: android android-intent broadcastreceiver vpn android-broadcast


【解决方案1】:

到目前为止我的发现

在棒棒糖中

android.net.conn.CONNECTIVITY_CHANGE 广播仅在 VPN 连接或断开连接时触发。

因此,您可以使用以下代码 sn-p 以及用于棒棒糖前设备的逻辑。

@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"));
   }

}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果有帮助,您可以在 onReceive 上尝试一下:

    @Override
    public void onReceive(Context context, Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
        // should check null because in air plan mode it will be null
        if (netInfo != null && netInfo.isConnected()) {
            System.out.println("INTERNET IS AVAILABLE");
        } else {
            System.out.println("INTERNET UNAVAILABLE");
        }
    }
    

    您可以将逻辑放在 if 语句中。

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2016-05-02
      相关资源
      最近更新 更多