2011-05-12 132 views
0

所以有一个不错的博客使用下面的代码识别应用安装

public class Installation { 
    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(); 
    } 
} 

在不同的Java类,我有一个的WebView去一个URL识别应用安装,如图

public class Notes extends Activity { 

    WebView mWebView; 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.timsnotes); 

     mWebView = (WebView) findViewById(R.id.webview3); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.loadUrl("http://www.somedomain.com/notes.php?phoneid=" + "sID" + ""); 
     mWebView.setWebViewClient(new HelloWebViewClient()); 
    } 

但所有它传递的是文本sID。有没有一个使用权限我缺少访问sID?

回答

0

而不是“sID”use Installation.id(this); 这将解析id到一个字符串。目前你传递的是“sID”,没有别的。