Android 游客登录根据机型判断不同用户

用户游客登录是根据获取手机设备码,并进行MD5加密后将字符串传入后台从而判断不同机型的用户,但是同一款手机的设备码相同从而造成同一型号的手机用户登录后是同一个ID
解决方案:使用设备码去区分

public class DeviceCodeUtils {

    public static String getUniqueDeviceId(boolean isReadCache) {
        String sbhcm = CacheUtil.readString(CacheUtil.getDeviceMD5Path(), "设备码");
        String sbm;
        if (isReadCache && !TextUtils.isEmpty(sbhcm)) {
            sbm = sbhcm;
        } else {
            sbm = DeviceUtils.getUniqueDeviceId();//无缓存  则生成
        }
        //如果本地无缓存则再次写入
        if (TextUtils.isEmpty(sbhcm))
            CacheUtil.writeString(CacheUtil.getDeviceMD5Path(), "设备码", sbm);

        return sbm;
    }

    public static String getDeviceMD5Path() {
        String storagePath = Environment.getExternalStorageDirectory() + "/." + AppContext.sInstance.getPackageName();
        return storagePath + "/final/device";
    }

}
package com.nightcolor.video.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.text.TextUtils;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.FileUtils;
import com.nightcolor.video.AppContext;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 缓存工具类
 * Author: zhangqingyou
 * Date: 2020/4/7
 * Des:
 */
public class CacheUtil {
    //系统包目录
    private static String systemPath;
    //存储空间隐藏目录.hjy
    private static String storagePath;
    //*******************************系统包目录********************************

    private static String getSystemPath() {
        if (TextUtils.isEmpty(systemPath))
            systemPath = AppContext.sInstance.getFilesDir().getAbsolutePath();
        return systemPath;
    }

    //用户json缓存目录
    public static String getUserPath() {
        return getSystemPath() + "/user";
    }

    //数据缓存目录
    public static String getPackageDataPath() {
        return getSystemPath() + "/data";
    }

    //任务记录缓存目录
    public static String getPackageJobPath() {
        return getSystemPath() + "/job";
    }

    //*******************************存储空间********************************

    private static String getStoragePath() {
        if (TextUtils.isEmpty(storagePath))
            storagePath = Environment.getExternalStorageDirectory() + "/.backup/" + AppContext.sInstance.getPackageName();
        return storagePath;
    }

    //缓存设备码 (不可清除)
    public static String getDeviceMD5Path() {
        return getStoragePath() + "/final/device";
    }

    //SD数据缓存目录(不可清除)
    public static String getFinalDataPath() {
        return getStoragePath() + "/final/data";
    }

    //其他数据缓存目录(可清除)
    public static String getStringDataPath() {
        return getStoragePath() + "/data";
    }

    //图片缓存目录(可清除)
    public static String getImagePath() {
        return getStoragePath() + "/image";
    }

    //下载缓存目录(可清除)
    public static String getDownloadPath() {
        return getStoragePath() + "/download";
    }


    /**
     * 写入信息
     */
    public static synchronized void writeString(String path, String key, String msg) {
        CacheDiskUtils.getInstance(new File(path)).put(key, new String(EncodeUtils.base64Encode(msg)));//加密写入缓存
    }

    /**
     * 写入信息(集合)
     */
    public static synchronized void writeStringList(String path, String key, List<String> stringList) {
        if (stringList != null) {
            String[] toArray = stringList.toArray(new String[0]);
            CacheDiskUtils.getInstance(new File(path)).put(key, toArray);
        }
    }

    /**
     * 读取信息
     */
    public static synchronized String readString(String path, String key) {
        String string = CacheDiskUtils.getInstance(new File(path)).getString(key);
        if (string == null)
            return null;
        return new String(EncodeUtils.base64Decode(string));//读取信息并解密
    }

    /**
     * 读取信息(集合)
     */
    public static synchronized List<String> readStringList(String path, String key) {
        Object serializable = CacheDiskUtils.getInstance(new File(path)).getSerializable(key);
        if (serializable != null && serializable instanceof String[]) {
            String[] toArray = (String[]) serializable;
            List list = Arrays.asList(toArray);
            List arrayList = new ArrayList(list);
            return arrayList;
        }
        return new ArrayList<>();
    }

    /**
     * 删除指定目录信息
     */
    public static synchronized void delteDirString(String path) {
        CacheDiskUtils.getInstance(new File(path)).clear();
    }

    /**
     * 删除指定目录指定key信息
     */
    public static synchronized void delteString(String path, String key) {
        CacheDiskUtils.getInstance(new File(path)).remove(key);
    }

    /**
     * 获取目录大小
     *
     * @param path
     * @return
     */
    public static synchronized String getDirSize(String path) {
        return FileUtils.getDirName(path);
    }


    /**
     * Context.MODE_PRIVATE:
     * 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,
     * 在该模式下,写入的内容key同名会覆盖原文件的内容
     * <p>
     * Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
     * <p>
     * Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
     * <p>
     * MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
     * <p>
     * MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
     *
     * @param context
     * @param sharedPreferencesName xml文件名
     * @param key
     * @param value
     */
    public static void savaSPString(Context context, String sharedPreferencesName, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
        sharedPreferences.edit().putString(key, value)
                .commit();
    }

    public static String getSPString(Context context, String sharedPreferencesName, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    public static void delteSP(Context context, String sharedPreferencesName) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
}

在Activity中调用 这个uuid是设备码,将uuid加密后传入后台接口即可

 final String uuid = DeviceCodeUtils.getUniqueDeviceId(true);
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值