如何通过下载管理器下载apk文件: 我试过这段代码:
public class UpdateCheckerService extends Service {
UpdateChecker update_checker;
DownloadManager downloadManager;
String Download_path="http://www.xxxxx.com/mobile_api/xxxxxx.apk";
SharedPreferences preferenceManager;
String Download_ID = "100";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
System.out.println("SERVICE ************ CREATED");
}
public void onStart(Intent intent1, int start_id)
{
System.out.println("Service started");
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, intentFilter);
System.out.println("Service started");
if(isNetworkAvailable())
{
if(true)
{
System.out.println("Update AVAAILLL:");
preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
Uri Download_Uri = Uri.parse(Download_path);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
long download_id = downloadManager.enqueue(request.setTitle("Downloading...")
.setDescription("xxxx").setMimeType("application/vnd.android.package-archive").setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"xxxxx.apk"));
Editor PrefEdit = preferenceManager.edit();
PrefEdit.putLong(Download_ID, download_id);
PrefEdit.commit();
}
}
}
public void onDestroy()
{ }
当我要安装.apk时,它会告诉解析错误。 并且在logcat中它告诉无法读取清单文件。 实际上.apk文件的大小是400kb,但下载后显示40kb。
请建议我错在哪里..
答案 0 :(得分:0)
没有办法做到这一点。这是为了安全 - 否则你会让每个网站都隐藏在人们面前安装应用程序。您可以下载apk然后使用文件管理器进行安装,但不能通过浏览器在一个操作中下载和安装。