android - 在 Android 上获取 VPN 连接状态

标签 android vpn

是否可以检查 Android 设备是否连接到 VPN 服务器? A search in the API为 Android 1.6 提供了“paltform highlights”,所以这并没有让我充满信心。

最佳答案

您可以注册到 broadcastreceiver,所有 vpn 状态都会出现在您的应用程序中。

将此添加到应用程序 list :

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

关于android - 在 Android 上获取 VPN 连接状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3461967/

相关文章:

android - 在android中保存和检索 View 数组

android - "Error writing minivpn binary "连接打开 vpn Android

c# - 是否有 SOAP 代理服务器之类的东西,还是我必须自己推出?

ruby-on-rails - GitLab 7-0 稳定无法推送或克隆

windows - OpenVPN 连接失败/此系统上的所有 TAP-Win32 适配器当前正在使用中

android - 在 AsyncTask 的 doInBackground() 中使用 OkHttp 库发出 HTTP 请求会阻止 UI 操作

android - 有没有办法为所有可用的操作系统获取当前运行的前台应用程序(包)?

android - 如何在标记下移动 map ?

android - Android 是否有任何 API 可以提供日期时间作为用户输入(startActivityForResult)?

android - 在 Android 中以编程方式基于 VpnService 创建 L2TP/IPSec VPN