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.