是否可以检查Android设备是否连接到VPN服务器? A search in the API为Android 1.6提供了“paltform highlight”,所以这并不能让我充满信心。
答案 0 :(得分:9)
您可以注册到broadcastreceiver,所有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
}
}
}