Android Network Status operation

Source: Internet
Author: User

  /**     * Declare Connection Manager Object *     /    private Connectivitymanager Connectivitymanager;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);       1. Get the Connection Manager Object        this.connectivitymanager= (Connectivitymanager) This.getsystemservice through system services ( Context.connectivity_service);       2. Obtain the Network information object through the Connection Manager Object        networkinfo networkinfo=this.connectivitymanager.getactivenetworkinfo ();       if (networkinfo==null) {           Toast.maketext (this, "current phone is not networked!", Toast.length_short). Show ();       else{           String typename=networkinfo.gettypename ();           System.out.println ("typename=" +typename);           Toast.maketext (This, "Typename=" +typename, Toast.length_short). Show ();       }    }

2.

    /** * Declare Connection Manager Object */private Connectivitymanager Connectivitymanager;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);       Setcontentview (R.layout.activity_main); 1. Get the Connection Manager Object This.connectivitymanager= (Connectivitymanager) This.getsystemservice through system services (context.connectivity_       SERVICE);        2. Obtain the Network information object through the Connection Manager Object Networkinfo networkinfo=this.connectivitymanager.getactivenetworkinfo ();            if (networkinfo.isavailable ()) {Toast.maketext (this, "There is a network!", Toast.length_short). Show ();            Get the network type int type= networkinfo.gettype (); Switch (type) {//mobile network case CONNECTIVITYMANAGER.TYPE_MOBILE:SYSTEM.OUT.PR                    INTLN ("mobile network");                Break                    WiFi Network case CONNECTIVITYMANAGER.TYPE_WIFI:SYSTEM.OUT.PRINTLN ("WiFi network");            Break }}eLSE {Toast.maketext (this, "No Network", Toast.length_short). Show (); }    }

  

Android Network Status operation

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.