Android development to obtain network link status
- 2020-06-07 05:16:07
- OfStack
Web development is a very important part of Android programming. Today, this article will share 1 point of Android web development experience with you.
This paper illustrates Android's method of obtaining network link status through an example. The details are as follows:
With current Android phones, there are five possible network states:
-- No network (this may be because the phone is down, the network is not on, the signal is not good, etc.)
-- Use WIFI
--CMWAP(China Mobile Agent)
- CMNET surf the Internet
- 2 G G / 3/4 G surf the Internet
There are many times when we need to determine whether the user has network Settings on, and we usually use the ConnectivityManager class to determine whether a network connection exists.
Access to network status:
So how do you actually use this class? How do you interact with users? Specific examples are as follows:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConnectivityManager nw = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = nw.getActiveNetworkInfo();
Toast.makeText(MainActivity.this, " The current network "+add(netinfo.isAvailable())+","+" network "+app(netinfo.isConnected())+","+" The network connection "+adp(netinfo.isConnected()), Toast.LENGTH_LONG).show();// Prompt the user for network status
}
String add(Boolean bl){
String s = " Do not use ";
if(bl==true){
s=" available ";
}
return s;
}
String app(Boolean bl){
String s = " Not connected ";
if(bl==true){
s=" The connected ";
}
return s;
}
String adp(Boolean bl){
String s = " Does not exist! ";
if(bl==true){
s=" There! ";
}
return s;
}
}
Of course, don't forget to get network permissions in the configuration file. The code is as follows:
<!-- Access to network -->
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
/>
I hope the examples in this article have been helpful in Android programming.