在 Android 上获取 VPN 连接状态

发布于 2024-09-13 22:07:54 字数 179 浏览 5 评论 0原文

是否可以检查 Android 设备是否连接到 VPN 服务器? API 中的搜索为 Android 1.6 提供“paltform 高亮显示” ,所以这并没有让我充满信心。

Is it possible to check if an Android device is connected to a VPN server?
A search in the API provides 'paltform highlights' for Android 1.6, so that doesn't fill me with much confidence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2024-09-20 22:07:54

您可以注册广播接收器,所有 VPN 状态都会出现在您的应用程序中。

将其添加到应用程序清单中:

<receiver android:name=".ConnectivityReceiver">
  <intent-filter>
    <action android:name="vpn.connectivity" />
  </intent-filter>
</receiver>

创建一个类:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ConnectivityReceiver extends BroadcastReceiver 
{
  public void onReceive(Context c, Intent intent) 
  {
    String state = intent.getSerializableExtra("connection_state").toString();

    Log.d("**************", state.toString());

    if (state.equals("CONNECTING")) {
      // Do what needs to be done
    }
    else if (state.equals("CONNECTED")) {
      // Do what needs to be done
    }
    else if (state.equals("IDLE")) {
      int errorCode = intent.getIntExtra("err", 0);
      if (errorCode != 0) {
        // Do what needs to be done to report a failure
      }
      else {
        // Normal disconnect
      }
    }
    else if (state.equals("DISCONNECTING")) {
      // Usually not very interesting
    }
  }
}

You can register to broadcastreceiver and all vpn states will come to you application.

Add this to application manifest:

<receiver android:name=".ConnectivityReceiver">
  <intent-filter>
    <action android:name="vpn.connectivity" />
  </intent-filter>
</receiver>

create a class:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ConnectivityReceiver extends BroadcastReceiver 
{
  public void onReceive(Context c, Intent intent) 
  {
    String state = intent.getSerializableExtra("connection_state").toString();

    Log.d("**************", state.toString());

    if (state.equals("CONNECTING")) {
      // Do what needs to be done
    }
    else if (state.equals("CONNECTED")) {
      // Do what needs to be done
    }
    else if (state.equals("IDLE")) {
      int errorCode = intent.getIntExtra("err", 0);
      if (errorCode != 0) {
        // Do what needs to be done to report a failure
      }
      else {
        // Normal disconnect
      }
    }
    else if (state.equals("DISCONNECTING")) {
      // Usually not very interesting
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文