[Original] Android determines whether the default data network is on. If it is off, the network configuration page is displayed.

Source: Internet
Author: User

Statement: This method is only used to determine the default data network, that is, the data network that needs to pay X yuan each month to obtain the Internet traffic of xx m. No WiFi, etc.


Step 1: add the permission to read the network status of the mobile phone

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Step 2: Check whether the default data network is enabled, and open the network configuration page when it is not enabled

Is a variety of network names and descriptions in Android. (Available under connectivitymanager)

We can see that type_mobile is the default data traffic network name of the mobile phone.

Therefore, we only need to determine whether the network is enabled.

// What to do when you check on posbtn? Private void checkonposbtn () {// todo force the user to set the network connected. connectivitymanager = (connectivitymanager) getsystemservice (context. connectivity_service); networkinfo = connectivitymanager. getnetworkinfo (connectivitymanager. type_mobile); If (false = networkinfo. isconnectedorconnecting () {opennetworksettings ();} else {// todo login logic} private void Opennetworksettings () {alertdialog dialog = new alertdialog. Builder (this). settitle ("enable Network Service"). setmessage ("this software requires network resources. Do you want to enable the network? "). Setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// go to the activity of settings of wirelessintent intenttonetwork = new intent ("/"); componentname = new componentname ("com. android. settings "," com. android. settings. wirelesssettings "); intenttonetwork. setcomponent (componentname); intenttonetwork. setaction ("android. intent. action. view "); startactivity (intenttonetwork); dialog. cancel ();}}). setnegativebutton ("no", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {dialog. cancel ();}}). show ();}

Where,

NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

In this sentence, getnetworkinfo (connctivitymanager. xx); this XX can have the type_mobile and type_wifi types listed above. You can modify them according to your actual network requirements.

References:

[1] explanation of how Android judges the network status

[2] Android determines the network status and opens the Network Settings dialog box when no network exists.

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.