Android connection first, to determine the status of the network, you need to have the appropriate permissions, the following is the permission code (ANDROIDMANIFEST.XML):
Copy Code code as follows:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.INTERNET"/>
Then, detect if network state is available
Copy Code code as follows:
/**
* Determine the status of the network connection
* @return True, available; false, not available
*/
Private Boolean isopennetwork () {
Connectivitymanager Connmanager = (connectivitymanager) getsystemservice (Context.connectivity_service);
if (connmanager.getactivenetworkinfo ()!= null) {
Return Connmanager.getactivenetworkinfo (). isavailable ();
}
return false;
}
Finally, open Network settings if not available
Copy Code code as follows:
/**
* Visit Baidu homepage, the network is not available to be set
*/
private void Initmoregames () {
String url_moregames = "http://www.baidu.com";
Mwebview = (webview) Findviewbyid (R.id.view_gamesort);
if (Mwebview!= null) {
Mwebview.requestfocus ();
WebSettings websettings = Mwebview.getsettings ();
if (websettings!= null) {
Websettings.setjavascriptenabled (TRUE);
Websettings.setcachemode (mode_private);
Websettings.setdefaulttextencodingname ("Utf-8");
}
Determine if the network is available
if (isopennetwork () = = True) {
Mwebview.loadurl (Url_moregames);
} else {
Alertdialog.builder Builder = new Alertdialog.builder (moregamesactivity.this);
Builder.settitle ("No Available Network"). Setmessage ("Do you want to set up your network?");
Builder.setpositivebutton ("Yes", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Intent Intent = null;
try {
String sdkversion = Android.os.Build.VERSION.SDK;
if (integer.valueof (sdkversion) > 10) {
Intent = new Intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else {
Intent = new Intent ();
ComponentName comp = new ComponentName ("Com.android.settings", "com.android.settings.WirelessSettings");
Intent.setcomponent (comp);
Intent.setaction ("Android.intent.action.VIEW");
}
MoreGamesActivity.this.startActivity (Intent);
catch (Exception e) {
LOG.W (TAG, "Open Network settings failed, please check ...");
E.printstacktrace ();
}
}
}). Setnegativebutton ("No", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Dialog.cancel ();
Finish ();
}
). Show ();
}
} else {
LOG.W (TAG, "Mwebview is null, please check ...");
}
}