簡體   English   中英

在Android上獲取VPN連接狀態

[英]Get VPN Connection status on Android

是否可以檢查Android設備是否連接到VPN服務器? API中的搜索為Android 1.6提供了“paltform highlight”,因此這並不能讓我充滿信心。

您可以注冊到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
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM