2013-11-25 105 views
2

下載.apk文件如何通過下載管理器下載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

有沒有辦法做到這一點。這是爲了安全 - 否則你會讓每個網站悄悄地在人們身上安裝應用程序。您可以下載apk,然後使用文件管理器進行安裝,但無法通過瀏覽器進行下載和安裝。

+0

我沒有通過瀏覽器進行安裝。我只想從我的服務器下載.apk文件到移動SD卡。 – savari