想要实现当连接需要验证类的WiFi时,打开浏览器功能最主要的是如何判断连接的wifi是需要验证的。
在网上搜和自己看原码终于找到了。
在frameworks/base/services/core/java/com/android/server/connectivity/NetworkMonitor.java 中有个isCaptivePortal()方法,可以模仿它改写成我们需要的。
第一步———注册一个广播监听wifi连接
public class WifiBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == ConnectivityManager.CONNECTIVITY_ACTION) {
Intent serviceIntent = new Intent(context,CheckWifiPortalService.class);
context.startService(serviceIntent);
}
}
}
第二步——监听到wifi连接时开启一个服务检测是否为需要验证wifi
第三步——服务中连接一个返回值204的网址,来判断是否需要验证
public class CheckWifiPortalService extends Service {
public CheckWifiPortalService(){}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(){
public void run(){
if (isWifiSetPortal()) {
Intent i = new Intent();
i.setAction("android.intent.action.VIEW");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri content_url = Uri.parse("http://www.baidu");
i.setData(content_url);
getApplicationContext().startActivity(i);
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
private boolean isWifiSetPortal() {
final String mWalledGardenUrl = "http://g/generate_204";
final int WALLED_GARDEN_SOCKET_TIMEOUT_MS = 10000;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(mWalledGardenUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setConnectTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
urlConnection.setUseCaches(false);
urlConnection.getInputStream();
return urlConnection.getResponseCode() != 204;
} catch (IOException e) {
return false;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
}
若为需要验证打开浏览器即可。
http://g/generate_204内容为空,如果访问成功则返回204
想要实现当连接需要验证类的WiFi时,打开浏览器功能最主要的是如何判断连接的wifi是需要验证的。
在网上搜和自己看原码终于找到了。
在frameworks/base/services/core/java/com/android/server/connectivity/NetworkMonitor.java 中有个isCaptivePortal()方法,可以模仿它改写成我们需要的。
第一步———注册一个广播监听wifi连接
public class WifiBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == ConnectivityManager.CONNECTIVITY_ACTION) {
Intent serviceIntent = new Intent(context,CheckWifiPortalService.class);
context.startService(serviceIntent);
}
}
}
第二步——监听到wifi连接时开启一个服务检测是否为需要验证wifi
第三步——服务中连接一个返回值204的网址,来判断是否需要验证
public class CheckWifiPortalService extends Service {
public CheckWifiPortalService(){}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(){
public void run(){
if (isWifiSetPortal()) {
Intent i = new Intent();
i.setAction("android.intent.action.VIEW");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri content_url = Uri.parse("http://www.baidu");
i.setData(content_url);
getApplicationContext().startActivity(i);
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
private boolean isWifiSetPortal() {
final String mWalledGardenUrl = "http://g/generate_204";
final int WALLED_GARDEN_SOCKET_TIMEOUT_MS = 10000;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(mWalledGardenUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setConnectTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
urlConnection.setUseCaches(false);
urlConnection.getInputStream();
return urlConnection.getResponseCode() != 204;
} catch (IOException e) {
return false;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
}
若为需要验证打开浏览器即可。
http://g/generate_204内容为空,如果访问成功则返回204