2011-06-02 68 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