=========================================================================================
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_network" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> </ScrollView> </LinearLayout>
代码:
package com.example.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.NetworkInfo; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private TextView tv_network; // 声明一个文本视图对象 private String desc = "开始侦听网络变更广播,请开关WLAN或者数据连接,再观察广播结果"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_network = findViewById(R.id.tv_network); tv_network.setText(desc); } @Override protected void onStart() { super.onStart(); networkReceiver = new NetworkReceiver(); // 创建一个网络变更的广播接收器 // 创建一个意图过滤器,只处理网络状态变化的广播 IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiver(networkReceiver, filter); // 注册接收器,注册之后才能正常接收广播 } @Override protected void onStop() { super.onStop(); unregisterReceiver(networkReceiver); // 注销接收器,注销之后就不再接收广播 } private NetworkReceiver networkReceiver; // 声明一个网络变更的广播接收器实例 // 定义一个网络变更的广播接收器 private class NetworkReceiver extends BroadcastReceiver { // 一旦接收到网络变更的广播,马上触发接收器的onReceive方法 @Override public void onReceive(Context context, Intent intent) { if (intent != null) { NetworkInfo networkInfo = intent.getParcelableExtra("networkInfo"); String networkClass = NetworkUtil.getNetworkClass(networkInfo.getSubtype()); desc = String.format("%s\n%s 收到一个网络变更广播,网络大类为%s," + "网络小类为%s,网络制式为%s,网络状态为%s", desc, DateUtil.getNowTime(), networkInfo.getTypeName(), networkInfo.getSubtypeName(), networkClass, networkInfo.getState().toString()); tv_network.setText(desc); } } } }
NetworkUtil:
package com.example.myapplication; import android.telephony.TelephonyManager; public class NetworkUtil { // 获取数据连接的制式类型 public static String getNetworkClass(int subType) { switch (subType) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return "2G"; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return "3G"; case TelephonyManager.NETWORK_TYPE_LTE: return "4G"; case TelephonyManager.NETWORK_TYPE_NR: return "5G"; default: return "未知"; } } }
DateUtil:
package com.example.myapplication; import android.annotation.SuppressLint; import android.text.TextUtils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @SuppressLint("SimpleDateFormat") public class DateUtil { // 获取当前的日期时间 public static String getNowDateTime(String formatStr) { String format = formatStr; if (TextUtils.isEmpty(format)) { format = "yyyyMMddHHmmss"; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date()); } // 获取当前的时间 public static String getNowTime() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); return sdf.format(new Date()); } // 获取当前的时间(精确到毫秒) public static String getNowTimeDetail() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); return sdf.format(new Date()); } public static String getNowDate() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); return sdf.format(new Date()); } public static String getDate(Calendar calendar) { Date date = calendar.getTime(); // 创建一个日期格式化的工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 将当前日期时间按照指定格式输出格式化后的日期时间字符串 return sdf.format(date); } public static String getMonth(Calendar calendar) { Date date = calendar.getTime(); // 创建一个日期格式化的工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); // 将当前日期时间按照指定格式输出格式化后的日期时间字符串 return sdf.format(date); } public static Date formatString(String strTime) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { date = sdf.parse(strTime); } catch (Exception e) { e.printStackTrace(); } return date; } }