2011-06-02 71 views
1

我有要求在除android市場或任何其他應用程序商店之外的一個公共站點上託管我的.apk文件。在Android市場中,註冊後市場中的下滑.apk將自動安裝在手機上,無需任何手動操作。因此,我願意創建一個URL並將我的.apk文件放入該文件中,並且要將.apk下載到android移動設備中,並且它必須自動安裝。在自己的網站上託管一個.apk文件

我該怎麼做......如果有任何代碼或鏈接正在重新編譯,請分享此鏈接。

回答

4

如果Android設備已設置 - >應用程序 - >未知來源選中,Android將允許.apk安裝。如果它沒有被檢查 - 你將不會成功。

假設檢查欄已勾選,並且您已下載.apk文件。您可以運行下面的代碼來觸發安裝:

Intent intent = new Intent(Intent.ACTION_VIEW); 
Uri uri = Uri.fromFile(new File(apkFileName)); 
intent.setDataAndType(uri, "application/vnd.android.package-archive"); 
startActivity(intent); 
+0

Thanku inazaruk我會嘗試感謝reponce – sailaja 2011-06-02 07:13:48

+3

我可能失去了一些東西簡單;但我如何運行此代碼(允許我將應用程序放到我的設備上),以便我可以將代碼添加到我的設備上? – 2012-12-06 18:23:27

2
{ 
     String url = "http://www.server.com/yourapp.apk"; 
     String PATH = Environment.getExternalStorageDirectory() + "/download/"; 
     File file = new File(PATH); 
     file.mkdirs(); 
     File outputFile = new File(file, "yourapp.apk"); 
     downloadFile(url, outputFile); 
     installApp(context); 
} 



private static void downloadFile(String url, File outputFile) { 
    try { 
     URL u = new URL(url); 
     URLConnection conn = u.openConnection(); 
     int contentLength = conn.getContentLength(); 

     DataInputStream stream = new DataInputStream(u.openStream()); 

     byte[] buffer = new byte[contentLength]; 
     stream.readFully(buffer); 
     stream.close(); 

     DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile)); 
     fos.write(buffer); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("FileNotFoundException",e+""); 
     return; 
    } catch (IOException e) { 
     Log.e("IOException",e+""); 
     return; 
    } 
} 


private static void installApp(Context mycontext) { 
    Intent installer = new Intent(); 
    installer.setAction(android.content.Intent.ACTION_VIEW); 
    String PATH = "file://" + Environment.getExternalStorageDirectory() + "/download/yourapp.apk"; 
    installer.setDataAndType(Uri.parse(PATH), "application/vnd.android.package-archive"); 
    mycontext.startActivity(installer); 
} 
+0

getContentLength返回由*響應頭*字段content-length指定的字節內容長度,如果未設置此字段,則返回-1。無法保證您將獲得正確的價值。 – 2012-03-26 19:19:10