【问题标题】:Setting a Unique ID at Application Install - Android在应用程序安装时设置唯一 ID - Android
【发布时间】:2012-02-07 14:15:16
【问题描述】:

我正在尝试实现下面找到的代码,以便在安装应用程序时为用户生成一个随机 ID 号。我只有几个问题。

  1. 如果我为此创建一个新文件 (Install.java),我如何访问另一个类中的 ID?
  2. 如何确保在首次安装应用程序时执行这部分程序?现在,程序从我的 Main.java 类开始(我是 Java 新手)。它会在安装应用程序时运行吗?

    public class Install {
    
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";
    
    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }
    
    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }
    
    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
    }
    

【问题讨论】:

  • 我敦促您不要写入文件(您不知道所有手机品牌和型号都可以使用哪些设备),而是使用 Preferences 或 SharedPreferences 对象。除此之外,您还可以利用 BackupManager,因此如果用户在另一部手机上安装应用程序,BackupManager 会将所有存储的首选项复制到新手机。
  • @RichardGreen 感谢您的回复。我现在正在尝试使用 SharedPreference 来实现这一点,但我遇到了很多麻烦。你能看看我的另一篇文章,告诉我在哪里以及如何在给定的代码中实现它吗?这是一个链接:stackoverflow.com/questions/9177092/…

标签: java android installation uuid


【解决方案1】:

这是我使用的一些代码 - 随意调整...

 public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.d(Tag, "Yay onCreate!"); // sorry sometimes I'm a bit verbose with my logs...
    createVerifierStrings();
    .....


 private void createVerifierStrings() {
    SharedPreferences prefs = this.getSharedPreferences("Someprefstringreference", 0);
    String not_set = "NOTSET";
    String android_key;
    android_key = prefs.getString("id", not_set);

    if (android_key.equals(not_set)) {
        Log.d(Tag, "Creating keys for 1st time");
        android_key = generateRandomEnoughStuff();
        prefs.edit().putString("id", android_key).commit();
    }
    ......

【讨论】:

  • 只是添加 - 你不能在初始化时生成一个字符串 - 你可以开始搞乱服务等,但你最好(我认为)在应用程序第一次运行时这样做.
【解决方案2】:

据我所知,您无法在安装完成后立即运行任意代码。

我认为你能得到的最接近的方法是在 MainActivity onCreate() 方法中进行检查,以确定这是否是第一次运行(检查这一点的好方法可能是获取对文件的引用并调用 file .exists(),得到的布尔值会告诉你是否需要创建 UID 文件。

【讨论】:

  • 所以在我的主要活动中包含这个而不是单独的类?
  • 或调用 Install.id 作为主要活动中的第一件事,如果您想将此逻辑保留在单独的类中。 (是的,我知道这条评论晚了 6 年)
【解决方案3】:

这是 Tim Bray 的一篇博文,它解释了您实际上应该做什么......

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 2022-07-08
    • 2021-02-07
    • 1970-01-01
    • 2015-09-12
    • 2015-10-22
    相关资源
    最近更新 更多