我有要求在除android市場或任何其他應用程序商店之外的一個公共站點上託管我的.apk
文件。在Android市場中,註冊後市場中的下滑.apk
將自動安裝在手機上,無需任何手動操作。因此,我願意創建一個URL並將我的.apk
文件放入該文件中,並且要將.apk
下載到android移動設備中,並且它必須自動安裝。在自己的網站上託管一個.apk文件
我該怎麼做......如果有任何代碼或鏈接正在重新編譯,請分享此鏈接。
我有要求在除android市場或任何其他應用程序商店之外的一個公共站點上託管我的.apk
文件。在Android市場中,註冊後市場中的下滑.apk
將自動安裝在手機上,無需任何手動操作。因此,我願意創建一個URL並將我的.apk
文件放入該文件中,並且要將.apk
下載到android移動設備中,並且它必須自動安裝。在自己的網站上託管一個.apk文件
我該怎麼做......如果有任何代碼或鏈接正在重新編譯,請分享此鏈接。
如果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);
{
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);
}
getContentLength返回由*響應頭*字段content-length指定的字節內容長度,如果未設置此字段,則返回-1。無法保證您將獲得正確的價值。 – 2012-03-26 19:19:10
Thanku inazaruk我會嘗試感謝reponce – sailaja 2011-06-02 07:13:48
我可能失去了一些東西簡單;但我如何運行此代碼(允許我將應用程序放到我的設備上),以便我可以將代碼添加到我的設備上? – 2012-12-06 18:23:27