Android唯一标识生成方案

来源:互联网 发布:程序员思维 编辑:程序博客网 时间:2024/05/17 07:34

友盟有个统计功能,每当你的App在新的设备上安装过,友盟的后台就能统计到新增设备当你卸载程序,再次安装。
友盟后台却能分辨出你这不是一台新的设备。
这个到底是怎么做到的,首先让我想到的是获取手机的唯一标识IMEI,并上传到服务器,对比判断是不是新的设备

IMEI的获取方法

TelephonyManager manager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVI CE);String imei = manager.getDeviceId();

这个确实是一个比较简单而又直接的方法,但是啊,这是个坑。因为有的厂商手机根本无法获取IMEI,有的获取的都是0
好在Android的提供一个生成唯一标识的API

 String uuid=java.util.UUID.randomUUID().toString();

但是我们不能每次程序启动都生成这样一个唯一标识吧,那这样就没有意义了,我们需要的是把这个唯一标识存起来
就算刷机,格式化内存卡,都有可能幸存下来的,这样就是确保它的唯一性

Android数据存储的四种方式
1,网络存储
2,内存卡存储
3,数据库存储
4,SharedPreferences

看着这些存储方式,哥虎躯一震,菊花以紧,为什么不用两种及两种以上存储结合使用。
假如,我们存SharedPreferences和内存卡(隐藏文件存储,后面会讲),当用户卸了App,内存卡的唯一标识还存在,用户格式化内存,我们存SharedPreferences依旧存在啊。
肯定会有骚年会问,如果用户格式化了内存卡同时删除了App怎么办,骚年,醒醒吧。
想要百分之百的唯一标识,那不太可能。
在文件存储路径上使用点奇淫巧技,可以大大提升文件被删除的可能性

Android系统上隐藏文件只需要在文件名前加一个点

String path="sdread/Android/.uuid";File file=new File(path);

前方高能,请各位女司机系好安全带,老司机忽略

import android.content.Context;import android.content.SharedPreferences;import android.os.Environment;import android.util.Log;import com.socks.library.KLog;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;/** * Created by xiongchengguang on 2016/10/13. */public class UUIDS {    private static final String TAG = UUIDS.class.getName();    private static UUIDS device;    private Context context;    private final static String DEFAULT_NAME = "system_device_id";    private final static String DEFAULT_FILE_NAME = "system_device_id";    private final static String DEFAULT_DEVICE_ID = "dervice_id";    private final static String FILE_ANDROID = Environment.getExternalStoragePublicDirectory("Android") + File.separator + DEFAULT_FILE_NAME;    private final static String FILE_DCIM = Environment.getExternalStoragePublicDirectory("DCIM") + File.separator + DEFAULT_FILE_NAME;    private static SharedPreferences preferences = null;    public UUIDS(Context context) {        this.context = context;    }    private String uuid;    public static UUIDS buidleID(Context context) {        if (device == null) {            synchronized (UUIDS.class) {                if (device == null) {                    device = new UUIDS(context);                }            }        }        return device;    }    public static String getUUID() {        if (preferences == null) {            Log.d(TAG, "Please check the UUIDS.buidleID in Application (this).Check ()");            return "dervice_id";        }        return preferences.getString("dervice_id", "dervice_id");    }    //生成一个128位的唯一标识符    private String createUUID() {        return java.util.UUID.randomUUID().toString();    }    public void check() {        preferences = context.getSharedPreferences(DEFAULT_NAME, 0);        uuid = preferences.getString(DEFAULT_DEVICE_ID, null);        if (uuid == null) {            if (checkAndroidFile() == null && checkDCIMFile() == null) {                uuid = createUUID();                saveAndroidFile(uuid);                saveDCIMFile(uuid);                Log.d(TAG, "new devices,create only id");            }            if (checkAndroidFile() == null) {                uuid = checkDCIMFile();                saveAndroidFile(uuid);                Log.d(TAG, "Android directory was not found in UUID, from the DCIM directory to take out UUID\n");            }            if (checkDCIMFile() == null) {                uuid = checkAndroidFile();                saveDCIMFile(uuid);                Log.d(TAG, "DCIM directory was not found in UUID, from the Android directory to take out UUID");            }            uuid = checkAndroidFile();            SharedPreferences.Editor editor = preferences.edit();            editor.putString(DEFAULT_DEVICE_ID, uuid);            editor.commit();            Log.d(TAG,"save uuid SharePref:" + uuid);        } else {            if (checkAndroidFile() == null) {                saveAndroidFile(uuid);            }            if (checkDCIMFile() == null) {                saveDCIMFile(uuid);            }        }        Log.d(TAG,"result uuid:" + uuid);    }    private String checkAndroidFile() {        BufferedReader reader = null;        try {            File file = new File(FILE_ANDROID);            reader = new BufferedReader(new FileReader(file));            return reader.readLine();        } catch (Exception e) {            return null;        } finally {            try {                if (reader != null) {                    reader.close();                }            } catch (Exception e) {                e.printStackTrace();            }        }    }    private void saveAndroidFile(String id) {        try {            File file = new File(FILE_ANDROID);            KLog.d(file);            FileWriter writer = new FileWriter(file);            writer.write(id);            writer.flush();            writer.close();        } catch (Exception e) {            e.printStackTrace();        }    }    private String checkDCIMFile() {        BufferedReader reader = null;        try {            File file = new File(FILE_DCIM);            reader = new BufferedReader(new FileReader(file));            return reader.readLine();        } catch (Exception e) {            return null;        } finally {            try {                if (reader != null) {                    reader.close();                }            } catch (Exception e) {                e.printStackTrace();            }        }    }    private void saveDCIMFile(String id) {        try {            File file = new File(FILE_DCIM);            KLog.d(file);            FileWriter writer = new FileWriter(file);            writer.write(id);            writer.flush();            writer.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

使用方法,传男不传女
创建App类继承Application

/** * Created by xiongchengguang on 2016/10/14. */public class App extends Application {    public void onCreate() {        super.onCreate();        UUIDS.buidleID(this).check();    }}

别忘记在AndroidManifest.xml的application节点下注册

 <application        ....省略        android:name=".App">        ....省略  </application>

如果我们向拿到UUID的值的话,方法如下,

 String uuid= UUIDS.getUUID();
1 0
原创粉丝点击