1.activity_main.xml
<TextView
android:id="@+id/nametextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="200dp"
android:textSize="20sp" />
<TextView
android:id="@+id/ipTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="100dp"
android:textSize="20sp" />
2.MainActivity.java,连接手机网络3G/4G/WIFI/无网络
public class MainActivity extends AppCompatActivity {
private TextView ipTextView = null;
private TextView nameTextView = null;
private ConnectivityManager mConnectivityManager = null;
private NetworkInfo mActiveNetInfo = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameTextView = (TextView) findViewById(R.id.nametextview);
ipTextView = (TextView) findViewById(R.id.ipTextView);
mConnectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE) ;//获取系统的连接服务
mActiveNetInfo = mConnectivityManager.getActiveNetworkInfo();//获取网络连接的信息
if(mActiveNetInfo==null)
myDialog();
else
setUpInfo();
}
//获取ip地址
public String getIPAddress() {
NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if ((info.getType() == ConnectivityManager.TYPE_MOBILE) || (info.getType() == ConnectivityManager.TYPE_WIFI) ){//当前使用2G/3G/4G网络
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
}
catch (SocketException e) {
e.printStackTrace();
}
}
}
else { //当前无网络连接,请在设置中打开网络
return null;
}
return null;
}
public void setUpInfo() {
if(mActiveNetInfo.getType()==ConnectivityManager.TYPE_WIFI) {
nameTextView.setText("网络类型:WIFI");
ipTextView.setText("IP地址:"+getIPAddress());
}
else if(mActiveNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {
nameTextView.setText("网络类型:3G/4G");
ipTextView.setText("IP地址:"+getIPAddress());
}
else {
nameTextView.setText("网络类型:未知");
ipTextView.setText("IP地址:");
}
}
private void myDialog() {
AlertDialog mDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("注意")
.setMessage("当前网络不可用,请检查网络!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
MainActivity.this.finish();
}
})
.create();//创建这个对话框
mDialog.show();//显示这个对话框
}
}
3.在AndroidManifest.xml文件里添置二个权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4.测试结果
3G/4G网络、wifi网络、无网络三种情况测试如下图: