package com.yw.sdkdemo.device; import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.telephony.CellInfo; import android.telephony.TelephonyManager; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.yw.sdkdemo.R; import com.yw.sdkdemo.airplane.AirPlaneActivity; import com.yw.tool.ToolsManager; import com.yw.util.DeviceManager; import java.util.List; /** * Created by xushuang on 2024/5/9 * E-mail: mitgic92@gmail.com * Description: */ public class DeviceInfoActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = DeviceInfoActivity.class.getSimpleName(); private Button bt_version; private Button bt_imei; private Button bt_location; private Button bt_sim; private Button bt_airphane; private Button bt_apn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_device_info); initView(); initListener(); } private void initView() { bt_version = (Button) findViewById(R.id.bt_version); bt_imei = (Button) findViewById(R.id.bt_imei); bt_location = (Button) findViewById(R.id.bt_location); bt_sim = (Button) findViewById(R.id.bt_sim); bt_airphane = (Button) findViewById(R.id.bt_airphane); bt_apn = (Button) findViewById(R.id.bt_apn); } private void initListener() { bt_version.setOnClickListener(this); bt_imei.setOnClickListener(this); bt_location.setOnClickListener(this); bt_sim.setOnClickListener(this); bt_airphane.setOnClickListener(this); bt_apn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_version: String baseVersion = ToolsManager.getInstance().getBaseVersion(); String custVersion = ToolsManager.getInstance().getCustVersion(); Toast.makeText(this, "baseVersion:" + baseVersion + " custVersion:" + custVersion, Toast.LENGTH_SHORT).show(); break; case R.id.bt_imei: Toast.makeText(this, "IMEI:" + getIMEI(DeviceInfoActivity.this), Toast.LENGTH_SHORT).show(); break; case R.id.bt_location: List list = getNearbyTowers(DeviceInfoActivity.this); StringBuilder content = new StringBuilder(); for (int i = 0; i < list.size(); i++) { content.append(list.get(i).toString()); } Toast.makeText(this, "基站信息 : " + content, Toast.LENGTH_SHORT).show(); break; case R.id.bt_sim: Toast.makeText(this, "SIM信息 : " + getSimInfo(DeviceInfoActivity.this), Toast.LENGTH_SHORT).show(); printSimState(DeviceInfoActivity.this); break; case R.id.bt_airphane: startActivity(new Intent(this, AirPlaneActivity.class)); break; case R.id.bt_apn: boolean ret = DeviceManager.setDefaultApn("china mobile cmmtm", "cmmtm"); Toast.makeText(this, "设置默认APN:" + ret, Toast.LENGTH_SHORT).show(); break; } } public String getIMEI(Context context) { String imei = null; try { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { imei = telephonyManager.getDeviceId(); } } catch (Exception e) { e.printStackTrace(); } return imei; } public List getNearbyTowers(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { return telephonyManager.getAllCellInfo(); } } return null; } public String getSimInfo(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // 获取SIM卡序列号(ICCID) String iccId = telephonyManager.getSimSerialNumber(); // 获取SIM卡提供商信息 String operatorName = telephonyManager.getSimOperatorName(); // 打印信息 System.out.println("SIM卡序列号(ICCID): " + iccId); System.out.println("SIM卡提供商: " + operatorName); return "SIM卡序列号(ICCID): " + iccId + "SIM卡提供商: " + operatorName; } public int getSimState(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getSimState(); } public void printSimState(Context context) { int simState = getSimState(context); String stateString; switch (simState) { case TelephonyManager.SIM_STATE_ABSENT: stateString = "无SIM卡"; break; case TelephonyManager.SIM_STATE_NETWORK_LOCKED: stateString = "需要网络解锁的SIM卡"; break; case TelephonyManager.SIM_STATE_PIN_REQUIRED: stateString = "需要PIN码解锁的SIM卡"; break; case TelephonyManager.SIM_STATE_PUK_REQUIRED: stateString = "需要PUK码解锁的SIM卡"; break; case TelephonyManager.SIM_STATE_READY: stateString = "SIM卡准备就绪"; break; case TelephonyManager.SIM_STATE_UNKNOWN: default: stateString = "未知的SIM卡状态"; break; } Toast.makeText(this, "SIM状态 : " + stateString, Toast.LENGTH_SHORT).show(); } }