在应用程序安装时设置唯一 ID - Android

发布于 2025-01-03 05:53:04 字数 1028 浏览 1 评论 0原文

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

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

    公共类安装{
    
    私有静态字符串 sID = null;
    私有静态最终字符串安装=“安装”;
    
    公共同步静态字符串id(上下文上下文){
        if (sID == null) {  
            文件安装 = new File(context.getFilesDir(), INSTALLATION);
            尝试 {
                if (!installation.exists())
                    写安装文件(安装);
                sID = readInstallationFile(安装);
            } catch (异常 e) {
                抛出新的运行时异常(e);
            }
        }
        返回sID;
    }
    
    private static String readInstallationFile(文件安装) throws IOException {
        RandomAccessFile f = new RandomAccessFile(安装, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(字节);
        f.close();
        返回新字符串(字节);
    }
    
    private static void writeInstallationFile(文件安装) throws IOException {
        FileOutputStream out = new FileOutputStream(安装);
        字符串id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        关闭();
    }
    }
    

I'm trying to implement the code found below so that I can generate a random ID number for the user right when the app is installed. I just have a couple questions.

  1. If I create a new file for this (Install.java) how do I access the ID in another class?
  2. How do I make sure that this part of the program is executed when the app is first installed? Right now, the program starts on my Main.java class (I'm new to Java). Will it just run when the app is installed?

    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();
    }
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2025-01-10 05:53:04

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

 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();
    }
    ......

Here's some code I use - feel free to adapt as you will...

 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();
    }
    ......
很糊涂小朋友 2025-01-10 05:53:04

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

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

As far as I know you don't get a way to run any arbitrary code right after installation is complete.

I think the closest you can get is make a check inside your MainActivity onCreate() method that determines whether or not this is the first run (a good way to check this might be to get a reference to your file and call file.exists(), the resulting boolean will tell you whether or not you need to create your UID file.

羁客 2025-01-10 05:53:04

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

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

Here is a blog post from Tim Bray that explains what you actually should be doing..

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文