理解这段代码

时间:2012-03-20 17:32:52

标签: java android sd-card uuid

需要帮助了解此代码实际输出的内容。是不是把文件放到了一个文件中? 我在http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

上找到了它
    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();
    }
}

代码完全如何在我的应用中发布。

package com.UUIID;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.util.Log;
import java.io.RandomAccessFile;
import java.util.UUID;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

public class UUIDActivity extends Activity {
    /** Called when the activity is first created. */
    TextView text;
    private static final String TAG = "Installation";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "program started");
        text = (TextView) findViewById(R.id.textfield);

    }

    class Installation {

        private String sID = null;
        private static final String INSTALLATION = "INSTALLATION";

        public synchronized String id(Context context) {
            if (sID == null) {
                File installation = new File(context.getFilesDir(),
                        INSTALLATION);
                try {

                    if (!installation.exists())
                        writeInstallationFile(installation);
                    Log.d(TAG, "Inside of installation If statement");
                    sID = readInstallationFile(installation);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            return sID;
        }

        private String readInstallationFile(File installation)
                throws IOException {
            RandomAccessFile f = new RandomAccessFile(installation, "r");
            byte[] bytes = new byte[(int) f.length()];
            f.readFully(bytes);
            Log.d(TAG, "Right before it calls f to close");
            f.close();
            return new String(bytes);
        }

        private void writeInstallationFile(File installation)
                throws IOException {
            FileOutputStream out = new FileOutputStream(installation);
            String id = UUID.randomUUID().toString();
            Log.d(TAG, "Right before the file gets written out.");
            out.write(id.getBytes());
            out.close();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

从我的观点来看,它返回安装的UUID,而不是“UUID到文件”,因为这样的东西不存在。

首次尝试获取安装的ID并将其存储在文件中时,会生成UUID,因此使用相同上下文的其他调用将返回先前生成的UUID。

答案 1 :(得分:1)

代码返回一个随机的UUID,它会持久保存到文件中。如果UUID已经生成,它会从文件中读取它,否则会随机创建它,然后将其保存到文件中

答案 2 :(得分:1)

public synchronized static String id(Context context)

返回持久性UUID(由UUID.randomUUID()生成)。换句话说,它每次都会返回相同的UUID。正如@Alonso Domiguez回答的那样,它可能是一个基于命名的安装ID。目标是为使用此代码的应用程序的每个实例提供唯一的ID。

这里的诀窍是

if (!installation.exists())
    writeInstallationFile(installation);
sID = readInstallationFile(installation);

功能:

writeInstallationFile(installation)

生成随机UUID,并将UUID写入硬编码文件。但是,它只会被调用一次;因为在第一次调用之后,!installation.exists()将始终为false(因为写UUID会创建该文件)。

相关问题