天天看點

Android 系統資訊擷取,包括IMEI、MEID、MAC等資訊

Android 系統資訊擷取,包括IMEI、MEID、MAC等資訊
Android 系統資訊擷取,包括IMEI、MEID、MAC等資訊
Android 系統資訊擷取,包括IMEI、MEID、MAC等資訊
Android 系統資訊擷取,包括IMEI、MEID、MAC等資訊

SystemInfoUtils.java

package example.com.baseknowledge.info;

import android.app.Service;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * ================================================
 * 作    者:zhoujianan
 * 版    本:v1.0
 * 建立日期:2020/6/17
 * 描    述:
 * 修訂曆史:
 * ================================================
 */
public class SystemInfoUtils {
    private static final String TAG = "SystemInfoUtils";

    public static Map<String, String> getInfo(Context context) {
        Map<String, String> infoMap = new LinkedHashMap<>();
        infoMap.put("裝置品牌名稱:", Build.BRAND);
        infoMap.put("裝置品牌名稱:", Build.BRAND);
        infoMap.put("裝置名稱:", Build.ID);
        infoMap.put("裝置的型号:", Build.MODEL);
        infoMap.put("裝置制造商:", Build.MANUFACTURER);
        infoMap.put("裝置主機闆名稱: ", Build.BOARD);
        infoMap.put("裝置硬體名稱:", Build.HARDWARE);
        infoMap.put("裝置驅動名稱:", Build.DEVICE);
        infoMap.put("Android ID:", Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID));
        infoMap.put("身份識别碼MEID:", getPhoneMEID(context));
        infoMap.put("裝置身份碼IMEI:", getPhoneIMEI(context));
        infoMap.put("序列号: ", getSerial());
        infoMap.put("網口 MAC:", getEth0MacAddress());
        infoMap.put("WIFI MAC:", getWiFiMacId(context));
        infoMap.put("裝置的唯一辨別:(由裝置的多個資訊拼接合成)", Build.FINGERPRINT);
        infoMap.put("Android版本:", Build.VERSION.RELEASE);
        infoMap.put("裝置版本:", Build.VERSION.INCREMENTAL);
        infoMap.put("核心版本:", getKernelVersion());
        return infoMap;
    }

    public static String getPhoneMEID(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        String meid = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.i(TAG, "getPhoneMEID: " + tm.getMeid());
            meid = tm.getMeid();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            meid = tm.getDeviceId();
        }
        return meid;
    }

    public static String getPhoneIMEI(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        List<String> imeiList = new ArrayList<>();
        StringBuffer imeiStr = new StringBuffer();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
                String imei = telephonyManager.getImei(slot);
                Log.i(TAG, "getPhoneIMEI: " + imei + " " + telephonyManager.getImei() + " " + telephonyManager.getDeviceId());
                imeiList.add(imei);
            }
        } else {
            imeiList.add(telephonyManager.getDeviceId());
        }
        if (imeiList != null) {
            int count = 0;
            for (String imei : imeiList) {
                if (count > 0) {
                    imeiStr.append("|");
                }
                imeiStr.append(imei);
                count++;
            }
        }
        return imeiStr.toString();
    }

    public static String getWiFiMacId(Context context) {
        String macAddress = "02:00:00:00:00:02";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StringBuffer buf = new StringBuffer();
            NetworkInterface networkInterface = null;
            try {
                networkInterface = NetworkInterface.getByName("wlan0");
                if (networkInterface == null) {
                    Log.i(TAG, "getWiFiMacId: networkInterface == null");
                    return macAddress;
                }
                byte[] addr = networkInterface.getHardwareAddress();
                for (byte b : addr) {
                    buf.append(String.format("%02X:", b));
                }
                if (buf.length() > 0) {
                    buf.deleteCharAt(buf.length() - 1);
                }
                macAddress = buf.toString();
            } catch (SocketException e) {
                e.printStackTrace();
                return macAddress;
            }
        } else {
            WifiManager mWifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (mWifi.isWifiEnabled()) {
                WifiInfo wifiInfo = mWifi.getConnectionInfo();
                macAddress = wifiInfo != null ? wifiInfo.getMacAddress() : "";
            }
        }
        return macAddress;
    }

    public static String getEth0MacAddress() {
        String macSerial = "";
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/eth0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();
                    Log.i(TAG, "getEth0MacAddress: " + macSerial);
                    break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return macSerial;
    }

    public static String getKernelVersion() {
        String kernelVersion = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            kernelVersion = Build.VERSION.BASE_OS;
        }
        if (TextUtils.isEmpty(kernelVersion)) {
            kernelVersion = System.getProperty("os.version");
        }
        return kernelVersion;
    }

    public static String getSerial() {
        String serial;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            serial = Build.getSerial();
        } else {
            serial = Build.SERIAL;
        }
        return serial;
    }

}

           

SystemHardwareInfoActivity.java

public class SystemHardwareInfoActivity extends AppCompatActivity {
    private RecyclerView infoRl;
    private InfoAdapter mInfoAdapter;
    private List<String> mInfoList;
    private static final String TAG = "SystemHardwareInfoActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_system_hardware_info);
        mInfoList = new ArrayList<>();
        infoRl = findViewById(R.id.infoRl);
        mInfoAdapter = new InfoAdapter(mInfoList);
        infoRl.setLayoutManager(new LinearLayoutManager(this));
        infoRl.setAdapter(mInfoAdapter);

        Map<String, String> info = SystemInfoUtils.getInfo(this);
        for (Map.Entry<String, String> i : info.entrySet()) {
            Log.i(TAG, i.getKey() +i.getValue());
            mInfoList.add(i.getKey() +i.getValue());
        }
        mInfoAdapter.notifyDataSetChanged();
    }
}

           

InfoAdapter.java

package example.com.baseknowledge.info;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import example.com.baseknowledge.R;

/**
 * ================================================
 * 作    者:zhoujianan
 * 版    本:v1.0
 * 建立日期:2020/6/17
 * 描    述:
 * 修訂曆史:
 * ================================================
 */
public class InfoAdapter extends RecyclerView.Adapter<InfoAdapter.MyViewHolder> {
    private List<String> mList;

    public InfoAdapter(List<String> list) {
        mList = list;
    }


    public InfoAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.info_item, parent, false);
        return new MyViewHolder(view) ;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
        holder.itemTextView.setText(mList.get(i));
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView itemTextView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            itemTextView = itemView.findViewById(R.id.info_tv);
        }
    }

}

           

activity_system_hardware_info.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".info.SystemHardwareInfoActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/infoRl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
           

info_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/info_tv"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:focusable="true"
    android:layout_marginTop="10dp"
    android:textSize="23sp">
</TextView>