需要幫助的種類瞭解此代碼實際輸出的內容。它是否把一個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();
}
}
}
好吧,所以我明白代碼將UUID寫入文件,如果它從未在手機上創建過。我搜索了我的手機SD卡,我沒有看到任何具有UUID的特定文件。但是,有一個文件夾無法讀取,UUID是否是專用的?從甚至用戶? – 2012-03-20 19:42:08