获得Android手机唯一标识并保存在本地及从本地读出

获得Android手机唯一标识并保存在本地及从本地读出

每台手机的唯一AndroidId

String AndroIdKey = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);

保存AndroidId到本地

 public static void saveBitmap() throws IOException {
   // 创建目录
   //获取内部存储状态  
   String state = Environment.getExternalStorageState();
   //如果状态不是mounted,无法读写  
   if (!state.equals(Environment.MEDIA_MOUNTED)) {
     return;
   }
   String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath();
   File appDir = new File(sdCardDir, "CaChe");
   if (!appDir.exists()) {
     appDir.mkdir();
   }
   String fileName = "dayu3421" + ".txt";
   file = new File(appDir, fileName);
   if (!file.exists()) {
     file.createNewFile();
   }
   //保存android唯一表示符
   try {
     FileWriter fw = new FileWriter(file);
     fw.write(MyApplication.getAndroIdKey());
     fw.flush();
     fw.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
 }

从本地读出来

public static String readKey() throws IOException {

  // 创建目录
  //获取内部存储状态  
  String state = Environment.getExternalStorageState();
  //如果状态不是mounted,无法读写  
  if (!state.equals(Environment.MEDIA_MOUNTED)) {
    return null;
  }
  String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath();
  File appDir = new File(sdCardDir, "CaChe");
  if (!appDir.exists()) {
    appDir.mkdir();
  }
  String fileName = "dayu3421" + ".txt";
  file = new File(appDir, fileName);
  if (!file.exists()) {
    file.createNewFile();
  }
   BufferedReader reader = null;
  StringBuilder content=null;
  try {
    FileReader fr = new FileReader(file);
    content= new StringBuilder();
    reader = new BufferedReader(fr);
    String line;
    while ((line= reader.readLine())!=null){
      content.append(line);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }finally {
    if (reader!=null){
      try {
        reader.close();
      }catch (IOException e){
        e.printStackTrace();
      }
    }
  }
 
  return content.toString();
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,581评论 25 708
  • Application 标签 android:allowTaskReparenting android:allow...
    Shawn_Dut阅读 7,945评论 2 61
  • 初来咋到,想象丰富,浮云落地,执着现在。或许,每个人在面临新事情,都有此心理状态。大学两年了,时不时会想,在...
    溪湖至白阅读 326评论 4 7
  • 三月末,春光正好。今日有阳光,便想着去到沙河边,找个茶馆坐下偷闲。 这家茶馆吸引我走进去的,是门口的几对茶客。他们...
    前度刘嫏阅读 225评论 0 1
  • 今天,百般无聊的我在卫生间中洗着脸,无意间那么一抬头,哇塞!快逃跑,我看见了一只怪物啊~~ 我看了看镜子中的我:且...
    印第安老麻雀阅读 154评论 0 0