Android Detection Network Connection Status Example explain _android

Source: Internet
Author: User

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 ...");
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.