Android determines the network status and opens the Network Settings dialog box when there is no network.

Source: Internet
Author: User

Android determines the network status and opens the Network Settings dialog box when there is no network.

When using Android to connect to the network, it is not always possible to connect to the network. At this time, we 'd better judge the network status when the program starts, if no network exists, you will be reminded to set it.

To determine the network status, you must first have the corresponding permissions. The permission code is as follows:

That is, access to the network status is allowed:

<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE">

The following is the judgment code:

Code

Private boolean NetWorkStatus (){

Boolean netSataus = false;

ConnectivityManager cwjManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );

CwjManager. getActiveNetworkInfo ();

If (cwjManager. getActiveNetworkInfo ()! = Null ){

NetSataus = cwjManager. getActiveNetworkInfo (). isAvailable ();

}

If (! NetSataus ){

Builder B = new AlertDialog. Builder (this). setTitle ("no available network ")

. SetMessage ("do you want to set the network? ");

B. setPositiveButton ("yes", new DialogInterface. OnClickListener (){

Public void onClick (DialogInterface dialog, int whichButton ){

Intent mIntent = new Intent ("/");

ComponentName comp = new ComponentName (

"Com. android. settings ",

"Com. android. settings. WirelessSettings ");

MIntent. setComponent (comp );

MIntent. setAction ("android. intent. action. VIEW ");

StartActivityForResult (mIntent, 0); // if you need to perform the operation again after the setting is complete, you can rewrite the operation code.

}

}). SetNeutralButton ("no", new DialogInterface. OnClickListener (){

Public void onClick (DialogInterface dialog, int whichButton ){

Dialog. cancel ();

}

}). Show ();

}

Return netSataus;

}

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.