布局优化,存储和获取DeviceID
This commit is contained in:
@ -3,6 +3,7 @@ package com.gh.common.util;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.os.Environment;
|
||||
|
||||
import com.gh.common.constant.Config;
|
||||
|
||||
@ -10,6 +11,10 @@ import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
@ -107,4 +112,135 @@ public class TokenUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void saveDeviceID(String deviceID, Context context){
|
||||
saveSharedPreferences(deviceID, context);
|
||||
saveDataFile(deviceID, context);
|
||||
svaeSDCard(deviceID, "");//SDCard根目录
|
||||
svaeSDCard(deviceID, "/system");//SDCard system目录
|
||||
svaeSDCard(deviceID, "/data");//SDCard data目录
|
||||
}
|
||||
|
||||
//将uuid存到sp
|
||||
private static void saveSharedPreferences(String deviceID, Context context) {
|
||||
SharedPreferences sp = context.getSharedPreferences("UUID", Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor edit = sp.edit();
|
||||
edit.putString("uuid", deviceID);
|
||||
edit.commit();
|
||||
Utils.log("saveDeviceID", "保存成功SP");
|
||||
}
|
||||
|
||||
//将uuid存到data/data/PackageName/files文件夹下
|
||||
private static void saveDataFile(String deviceID, Context context) {
|
||||
FileOutputStream fops;
|
||||
try {
|
||||
fops = context.openFileOutput("uuid", Context.MODE_PRIVATE);
|
||||
fops.write(deviceID.getBytes());
|
||||
fops.close();
|
||||
Utils.log("saveDeviceID", "保存成功DataFile");
|
||||
} catch (Exception e) {
|
||||
Utils.log("保存uuid到data/data/PackageName/files文件异常" + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//将uuid存到SD卡
|
||||
private static void svaeSDCard(String deviceID,String saveDir) {
|
||||
File sdCardDir = Environment.getExternalStorageDirectory();
|
||||
String path = sdCardDir.getPath() + saveDir + "/gh-uuid";
|
||||
|
||||
File file = new File(path);
|
||||
if (!file.exists()){
|
||||
file.mkdir();
|
||||
}
|
||||
|
||||
File writeFile = new File(file, "uuid");
|
||||
FileOutputStream fos;
|
||||
try {
|
||||
fos = new FileOutputStream(writeFile);
|
||||
fos.write(deviceID.getBytes());
|
||||
fos.close();
|
||||
Utils.log("saveDeviceID", "保存成功SDCard"+"目录为:"+saveDir);
|
||||
} catch (Exception e) {
|
||||
Utils.log("保存uuid到SDCard异常" + saveDir + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String getDeviceID(Context context){
|
||||
loadDataFile(context);
|
||||
loadSDCard("");
|
||||
loadSDCard("/system");
|
||||
loadSDCard("/data");
|
||||
return loadSharedPreferences(context);
|
||||
}
|
||||
|
||||
//读取SharedPreferences的uuid
|
||||
private static String loadSharedPreferences(Context context) {
|
||||
SharedPreferences sp = context.getSharedPreferences("UUID", Context.MODE_PRIVATE);
|
||||
String uuid = sp.getString("uuid", "-1");
|
||||
if ("-1".equals(uuid)){
|
||||
return loadDataFile(context);
|
||||
}
|
||||
Utils.log("getDeviceID", "获取成功SP" + uuid);
|
||||
return "";
|
||||
}
|
||||
|
||||
//读取data/data/PackageName/files的uuid
|
||||
private static String loadDataFile(Context context) {
|
||||
File file = new File(context.getFilesDir(), "uuid");
|
||||
if (file.exists()){
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
byte[] b = new byte[1024];
|
||||
int count = -1;
|
||||
String uuid = "";
|
||||
while ((count = fis.read(b)) != -1) {
|
||||
uuid = new String(b, 0, count, "UTF-8");
|
||||
}
|
||||
Utils.log("getDeviceID", "获取成功DataFile"+ uuid);
|
||||
return uuid;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else {
|
||||
|
||||
String[] dirName = {"", "/system", "/data"};
|
||||
for (int i = 0; i<3; i++){
|
||||
String s = loadSDCard(dirName[i]);
|
||||
if (!s.isEmpty()){
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
//读取SD卡的uuid
|
||||
private static String loadSDCard(String saveDir){
|
||||
File sdCardDir = Environment.getExternalStorageDirectory();
|
||||
String path = sdCardDir.getPath() + saveDir + "/gh-uuid";
|
||||
File file = new File(path, "uuid");
|
||||
if (file.exists()){
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
byte[] array = new byte[1024];
|
||||
int len = -1;
|
||||
while( (len = fis.read(array)) != -1){
|
||||
bos.write(array,0,len);
|
||||
}
|
||||
bos.close();
|
||||
fis.close();
|
||||
Utils.log("getDeviceID", "获取成功SDCard"+"目录为:"+saveDir+"::"+bos.toString());
|
||||
return bos.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user