Network state Inference for Android notes

Source: Internet
Author: User

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

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.