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: