您的当前位置:首页正文

DataProvider为项目提供基础信息

2024-11-08 来源:个人技术集锦
基础信息提供者接口
public interface IDataProvider {

    /**
     * 项目环境类型(开发,测试,正式)
     *
     * @return
     */
    Environment getAppEnvironment();

    /**
     * 项目context对象
     *
     * @return
     */
    Context getContext();

    /**
     * 项目名称
     *
     * @return
     */
    String getAppName();

    /**
     * 项目包名
     *
     * @return
     */
    String getPackageName();

    /**
     * 项目版本号
     *
     * @return
     */
    String getVersionName();


    /**
     * 手机imei
     *
     * @return
     */
    String getImei();

    /**
     * 手机imsi
     *
     * @return
     */
    String getImsi();

    /**
     * 手机型号
     *
     * @return
     */
    String getModel();

    /**
     * 手机id
     *
     * @return
     */
    String getPhoneId();

    /**
     * 获取网络状态类
     * @return
     */
    INetState getNetState();

    /**
     * 网络类型
     *
     * @return
     */
    int getNetType();

    /**
     * 手机内存大小
     *
     * @return
     */
    int getRamSize();

    /**
     * 手机屏幕宽度
     *
     * @return
     */
    int getScreenWidth();

    /**
     * 手机屏幕高度
     *
     * @return
     */
    int getScreenHeight();

    /**
     * 手机地理信息
     *
     * @return
     */
    Location getLocation();

}
基础信息提供者
public class DataProvider implements IDataProvider {

    private Context mContext;

    private INetState mINetState;

    public DataProvider(Context context) {
        this.mContext = context.getApplicationContext();
        this.mINetState = new NetState(mContext);
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public String getAppName() {
        return mContext.getString(R.string.app_name);
    }

    @Override
    public String getPackageName() {
        return TextUtils.isEmpty(mContext.getPackageName()) ? "" : mContext.getPackageName();
    }

    @Override
    public String getVersionName() {
        return AndrUtils.getVersionName(mContext);
    }

    @Override
    public String getImei() {
        return AndrUtils.getIMEI(mContext);
    }

    @Override
    public String getImsi() {
        return AndrUtils.getIMSI(mContext);
    }

    @Override
    public String getPhoneId() {
        return AndrUtils.getPhoneId(mContext);
    }

    @Override
    public String getModel() {
        return AndrUtils.getModel();
    }

    @Override
    public int getRamSize() {
        return AndrUtils.getRamSize(mContext);
    }

    @Override
    public INetState getNetState() {
        return mINetState;
    }

    @Override
    public int getNetType() {
        return mINetState.getNetType();
    }

    @Override
    public int getScreenWidth() {
        return AndrUtils.getWidthPixels(mContext);
    }

    @Override
    public int getScreenHeight() {
        return AndrUtils.getHeightPixels(mContext);
    }

    @Override
    public Location getLocation() {
        Double[] doubles = Util.getLocation(mContext);
        Location location = new Location(String.valueOf(doubles[0]), String.valueOf(doubles[1]));
        return location;
    }

    @Override
    public Environment getAppEnvironment() {
        return Environment.TEST;
    }

}
信息管理者
public class DataProviderManager {

    private static Context applicationContext;

    private static IDataProvider mDataProvider;

    public static Context getContext() {
        if (applicationContext != null) {
            return applicationContext;
        } else {
            if (mDataProvider != null && mDataProvider.getContext() != null) {
                applicationContext = mDataProvider.getContext();
            } else {
                applicationContext = getSystemApp();
            }
            return applicationContext;
        }
    }

    public static IDataProvider getDataProvider() {
        return mDataProvider;
    }

    public static void setDataProvider(IDataProvider mDataProvider) {
        DataProviderManager.mDataProvider = mDataProvider;
    }

    private static Application getSystemApp() {
        try {
            return (Application) Class.forName("android.app.ActivityThread")
                    .getMethod("currentApplication")
                    .invoke(null, (Object[]) null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
INetState网络状态接口
public interface INetState extends INetChangeListener{

    int NETWORN_NONE = 0; // 无网络
    int NETWORN_WIFI = 1; // wifi
    int NETWORN_2G = 2; // 2g网络
    int NETWORN_3G = 3; // 3g网络
    int NETWORN_4G = 4; // 4g网络
    int NETWORN_MOBILE = 5; // 手机网络

    /**
     * 获取网络类型
     *
     * @return
     */
    int getNetType();

    /**
     * 网络是否连接
     *
     * @return
     */
    boolean isNetworkConnected();

    /**
     * 是否2G
     *
     * @return
     */
    boolean is2G();

    /**
     * 是否3G
     *
     * @return
     */
    boolean is3G();

    /**
     * 是否Wifi
     *
     * @return
     */
    boolean isWifi();

    /**
     * 是否移动网络
     *
     * @return
     */
    boolean isMobileConnected();

    /**
     * GPS是否可用
     *
     * @return
     */
    boolean isGPSEnabled();

}
网络改变接口
public interface INetChangeListener {

    /**
     * 网络改变
     *
     * @param context
     */
    void onNetChange(Context context);
}
网络改变广播
public class NetStateReceiver extends BroadcastReceiver {

    /**
     * 未连接
     */
    private static final int NOTCONNECTED = -1;

    /**
     * 通过该标记解决网络变化时,广播通知会发送多次的问题
     */
    private static int lastType = NOTCONNECTED;

    private INetState netState;

    @Override
    public void onReceive(Context context, Intent intent) {
        netState = DataProviderManager.getDataProvider().getNetState();
        if (netState == null) {
            return;
        }
        NetworkInfo networkInfo = getActiveNetworkInfo(context);
        if (networkInfo != null && networkInfo.isConnected()) {
            int currentType = networkInfo.getType();
            if (currentType != lastType) {
                lastType = currentType;
                change(context);
            }
        } else {
            if (lastType != NetStateReceiver.NOTCONNECTED) {
                lastType = NetStateReceiver.NOTCONNECTED;
                change(context);
            }
        }
    }

    /**
     * 网络改变
     * @param context
     */
    private void change(Context context) {
        netState.onNetChange(context);
        L.d("网络改变:当前网络状态" + netState.getNetType());
    }

    /**
     * 获取当前已激活的网络链接信息
     *
     * @param context 应用程序上下文
     * @return 返回当前已激活的网络链接信息
     */
    private NetworkInfo getActiveNetworkInfo(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.getActiveNetworkInfo();
    }
在自定义Application里初始化
DataProviderManager.setDataProvider(new DataProvider(this));

代码调用:获取context

DataProviderManager.getContext();
获取设备号
DataProviderManager.getDataProvider().getPhoneId();







显示全文