1, first of all, of course, in the manifest to add check network status permissions;
<uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permissionAndroid:name= "Android.permission.INTERNET"/>
2, the Concrete Judgment step (1) First of all to get Android to the connection management of a class object.
Connectivitymanager manager = (Connectivitymanager) getsystemservice (
Context.connectivity_service);
(2) Get the network link status if (manager.getactivenetworkinfo () = null) {Networkflag = Manager.getactivenetworkinfo (). isavailable (); }
(3) If there is no connection, the popup dialog prompts the user to connect, and in the "Settings" button let the user jump to set the network interface. if (! Networkflag) { Alertdialog.builder Builder = new Alertdialog.builder (this); builder.seticon (Android. R.drawable.ic_dialog_alert); builder.settitle ("network status"); builder.setmessage ("The current network is not available, do you want to set up a network?" "); builder.setpositivebutton (" Settings ", New Dialoginterface.onclicklistener () { . @Override public void OnClick (Dialoginterface dialog, I NT which) { //TODO auto-generated method stub& nbsp Intent wifisettingsintent = new Intent ("Android.settings.WIFI_settings "); startactivity ( wifisettingsintent); } &NBS P }); builder.setnegativebutton (R.string.cancel, new Dialoginterface.onclicklistener () { &NBS P @Override public void oncli CK (dialoginterface dialog, int which) { dialog. Cancel (); } }) ; builder.create (); builder. Show (); }
(4) The final step, but also a more critical step, is to declare the activity in the manifest, if not done, it will appear "XXX has stopped running", the reason is obvious. The problem is, we used to build our own activity, know how to declare it, this is the system's own setting, how do we declare it?
<preferencescreen android:title= "Set up network" android:summary= "" > <intent android:action= "Android.intent.action.MAIN" android:targetpackage= "Com.android.settings" android:targetclass= "Com.android.settings.WirelessSettings"/> </PreferenceScreen>
Write the above code in the application. (Puzzled for a long time, finally in a senior blog found the answer, thank you, http://blog.csdn.net/shenyuemei/article/details/8005241)
Because the code is simple, the demo code is not uploaded.