1

如何通过下载管理器下载 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。

请提出我错在哪里..

4

1 回答 1

0

没有办法做到这一点。这是为了安全 - 否则你会让每个网站都在人们身上秘密安装应用程序。您可以下载 apk,然后使用文件管理器进行安装,但您无法通过浏览器一次性下载和安装。

于 2013-11-25T13:05:38.457 回答