Android 开发相关文章收集Android知识Android开发

DeviceInfo采集android设备信息工具

2018-02-01  本文已影响0人  appzy

库介绍

功能

采集Android设备信息,以Json形式输出

可自由定制要采集的设备类型,显示的设备信息详情等

特性

输出示例

{
    "board": {"boardName": "MSM8939"},
    "sim": [{
        "dataState": "0",
        "imsi": "460036820263837",
        "isNetworkRoaming": "false",
        "networkOperatorName": "China Telecom",
        "networkType": "14",
        "phoneType": "2",
        "simCountryIso": "cn",
        "simOperator": "46003",
        "simSerialNumber": "89860315844110607274",
        "simState": "0"
    }]
}
DeviceInfoManager.NewInstance(this)
        .addCollector(new PhoneBasicInfoCollector(this, "basic"))       //Andorid设备基本信息(PhoneBasicInfoCollector)
        .addCollector(new SimInfoCollector(this, "sim"))                //Sim卡信息(SimInfoCollector)同时识别多张Sim卡
        .addCollector(new CpuInfoCollector(this, "cpu"))                //Cpu信息(CpuInfoCollector)
        .addCollector(new BoardInfoCollector(this, "board"))            //主板信息(BoardInfoCollector)
        .addCollector(new BatteryInfoCollector(this, "battery"))        //电池信息(BatteryInfoCollector)
        .addCollector(new StorageInfoCollector(this, "storage"))        //存储信息(RAM & SD)(StorageInfoCollector)
        .addCollector(new CameraInfoCollector(this, "camera", true))    //摄像头信息(CameraInfoCollector)
        .addCollector(new ScreenInfoCollector(this, "screen"))          //屏幕信息(ScreenInfoCollector)
        .addCollector(new UiInfoCollector(this, "ui"))                  //Ui信息(UiInfoCollector)
        .addCollector(new SensorInfoCollector(this, "sensor"))          //传感器列表(SensorInfoCollector)
        .addCollector(new NfcInfoCollector(this, "nfc"))                //NFC信息(NfcInfoCollector)
        .addCollector(new SystemInfoCollector(this, "system"))          //系统相关信息(Build.prop等)
        .autoStartManualCollection(true)
        .bindListener(mDeviceInfoCollectListener)
        .start();

目前可获取的设备信息(只作为模板用途,建议使用时自行定制)

如何使用

添加依赖库

[https://jitpack.io]
[GitHub issues]

库版本号 version 请看上方 JitPack 的最新版本号,如v1.0.2,并不是 *xxx* (推荐使用最新版)

Gradle

Maven

使用方法

拓展自定义Collector

通过继承BaseDeviceInfoCollector抽象类进行功能拓展,以采集更多信息,或输出自定义的Json内容

package ltns.deviceinfolib.collector;

import android.content.Context;

/**
 * @date 创建时间:2018/1/8
 * @author appzy
 * @Description
 * @version
 */

public class DemoCollector extends BaseDeviceInfoCollector {
    public DemoCollector(Context context, String collectorName) {
        super(context, collectorName);
    }

    @Override
    public boolean needCollectManually() {
        return false;
    }

    @Override
    public String[] getRequiredPermissions() {
        return new String[0];
    }

    @Override
    protected void doCollectAutomatically() {

    }

    @Override
    protected void doCollectManually() {

    }
}

通过Manager管理多个Collector

为了代码简洁易读,我把它设计成这样:(不知道这样写会不会违背什么设计模式,如果有请issues告诉我,谢谢:D)

DeviceInfoManager.NewInstance(this)
                .addCollector(new BoardInfoCollector(MainActivity.this, "board"))
                .addCollector(new SimInfoCollector(MainActivity.this, "sim"))
                .autoStartManualCollection(true)
                .bindListener(mDeviceInfoCollectListener)
                .start();

在DeviceInfoCollectListener中监听采集状态:

注意:onSingleFailure(BaseDeviceInfoCollector mCollector, String mErrorInfo)中也可能存在数据,即mCollector.getJsonInfo();也可能会有数据,具体原因可参考源码和下方 注意事项

    private DeviceInfoCollectListener mDeviceInfoCollectListener = new DeviceInfoCollectListener() {
        @Override
        public void onStart() {
        }

        @Override
        public void onSingleSuccess(BaseDeviceInfoCollector mCollector) {
            //mCollector.getJsonInfo();
        }

        @Override
        public void onSingleFailure(BaseDeviceInfoCollector mCollector, String mErrorInfo) {
            //mCollector.getJsonInfo();
        }

        @Override
        public void onAllDone(DeviceInfoManager mDeviceInfoManager) {
            // mDeviceInfoManager.getDeviceJsonInfo();
        }

        @Override
        public void onAutoAllDone(DeviceInfoManager mDeviceInfoManager) {
        }
    };

Collector单独跑也是支持的

(支持但不推荐)

当然还是推荐使用Manager :D。就是想要自己单独跑也可以,接着~

特别说明:Collector单独跑需要调用者自行申请权限,因此方便起见,推荐使用Manager统一管理

BoardInfoCollector为例:

BoardInfoCollector mCollector = new BoardInfoCollector(MainActivity.this, "board");
        mCollector.bindObserver(mStateObserver);
        mCollector.startCollectAutomatically();//启动自动采集
        mCollector.startCollectManually();//启动手动采集

CollectorStateObserver中监听Collector状态:

    private CollectorStateObserver mStateObserver=new CollectorStateObserver() {
        @Override
        public void onCollectionSuccess(BaseDeviceInfoCollector mCollector) {

        }

        @Override
        public void onCollectionFailure(BaseDeviceInfoCollector mCollector, String mErrorInfo) {

        }

        @Override
        public void onManualCollectionSuccess(BaseDeviceInfoCollector mCollector, boolean startNext) {

        }

        @Override
        public void onManualCollectionFailure(BaseDeviceInfoCollector mCollector, String mErrorInfo, boolean startNext) {

        }

        @Override
        public void onNeedManualCollect(BaseDeviceInfoCollector mCollector) {

        }
    };

注意事项

更新日志

感谢

扯扯淡

上一篇下一篇

猜你喜欢

热点阅读