Network, not every time you can connect to the network, so in the program startup needs to determine the status of the network, if there is no network to remind users to set
Android connection first, to determine the network status, you need to have the appropriate permissions, the following is the permission code (ANDROIDMANIFEST.XML):
Copy CodeThe code is as follows:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.INTERNET"/>
Then, detect if the network status is available
Copy CodeThe code is as follows:
/**
* To 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, the network setting is turned off
Copy CodeThe code is as follows:
/**
* Visit Baidu homepage, the network is not available, you need to 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 Network Available"). Setmessage ("Do you want to set the 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 ...");
}
}