Note: Determine whether the device is connected to IOT platform !, Note: Device networking

Source: Internet
Author: User

Note: Determine whether the device is connected to IOT platform !, Note: Device networking

Remember to add permissions:

1 package com. lixu. getInternet; 2 3 import android. app. activity; 4 import android. content. context; 5 import android.net. connectivityManager; 6 import android.net. networkInfo; 7 import android. OS. bundle; 8 import android. widget. toast; 9 10 public class MainActivity extends Activity {11 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setCont EntView (R. layout. activity_main); 16 17 boolean B = isConnect (this); 18 19 if (B = true) 20 Toast. makeText (this, "your mobile phone is connected to the Internet! ", Toast. LENGTH_LONG). show (); 21 22 else if (B = false) 23 Toast. makeText (this," your phone is not connected to IOT platform! ", Toast. LENGTH_LONG ). show (); 24 25} 26 27 public boolean isConnect (Context context) {28 ConnectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); 29 if (cm! = Null) {30 NetworkInfo info = cm. getActiveNetworkInfo (); 31 if (info! = Null & info. isConnected () {32 if (info. getState () = NetworkInfo. state. CONNECTED) {33 return true; 34} 35} 36} 37 return false; 38} 39 40}

Running effect:

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.