是否可以检查 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/