1, first of all, of course, in the manifest to check the status of network access;
<uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permissionAndroid:name= "Android.permission.INTERNET"/>
2. Detailed inference step (1) First of all, 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, which is a key step, is to declare the activity in manifest. If you do not, there will be "XXX has stopped execution" for obvious reasons. The problem is that we used to build our own activity and know how to declare it. Here is the system of its own setting, how can 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>
You can write the above code in 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.
Network state Inference for Android notes