如何解决DownloadManager的状态为未决?

时间:2018-07-11 10:25:22

标签: android android-download-manager

我正在尝试使用DownloadManager类在我的Android应用中制作一个下载文件,并将某些Toast置于下载状态。当我单击下载按钮时,Toast总是说我的下载正在等待中。为什么会发生?

DownloadManager documnet中,我读到“未决”表示下载正在等待开始。还等什么呢?我应该在哪里找?如何更改状态并开始下载?

这是我的代码:

public void downloadFile(){
        Uri uri = Uri.parse("https://kopi81.000webhostapp.com/cccc.jpg");
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS,
                "cccc.jpg");
        Long ref = downloadManager.enqueue(request);

        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(ref);
        Cursor cursor = downloadManager.query(query);
        if(cursor.moveToFirst()){
            DownloadStatus(cursor, ref);
        }
    }

我认为我的互联网也没问题。我要获取的文件来自服务器上的 000webhost.com 。当我尝试从另一台服务器获取任何文件时,下载开始。是我的服务器出了问题还是我的android应用程序?

1 个答案:

答案 0 :(得分:2)

我为您编写了此代码。希望对您有帮助❤:

private DownloadManager downloadManager;
private Uri Download_Uri;
private long refid;
public void downloadFile(){

        registerReceiver(onComplete,
        new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Download_Uri = Uri.parse("https://kopi81.000webhostapp.com/cccc.jpg");
        DownloadManager.Request req = new DownloadManager.Request(Download_Uri);
                req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                req.setAllowedOverRoaming(false);
                req.setTitle("Downloading ... " );
                req.setDescription("Downloading " + "cccc.jpg");
                req.setVisibleInDownloadsUi(true);
                req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/Kopi81/"  + "cccc.jpg");
                refid = downloadManager.enqueue(req);

                }


    BroadcastReceiver onComplete = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

               Toast.makeText(context,"Download completed !",Toast.LENGTH_SHORT).show();
                NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MainActivity.this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Kopi81")
                .setContentText("Download completed !");
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(455, mBuilder.build());


        }
    };}
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我在[Sony Z3]和[Sony XZ]设备上进行了测试,没有问题!

祝你好运。