Network status judgment for Android notes

Source: Internet
Author: User

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.

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.