Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何接收下载管理器意图的状态,直到下载成功或失败_Android_Android Asynctask_Download Manager - Fatal编程技术网

Android 如何接收下载管理器意图的状态,直到下载成功或失败

Android 如何接收下载管理器意图的状态,直到下载成功或失败,android,android-asynctask,download-manager,Android,Android Asynctask,Download Manager,这是我的问题。 我正在尝试通过Asynctask使用下载管理器意图从服务器下载文件。 在我的asynctask类的doInBackground中,我调用download manager intent,当下载完成(成功或失败)时,doInBackground将返回布尔值。 这是我的密码 protected Boolean doInBackground(String... f_url) { boolean flag = true; boolean download

这是我的问题。 我正在尝试通过Asynctask使用下载管理器意图从服务器下载文件。 在我的asynctask类的doInBackground中,我调用download manager intent,当下载完成(成功或失败)时,doInBackground将返回布尔值。 这是我的密码

  protected Boolean doInBackground(String... f_url) {
        boolean flag = true;
        boolean downloading =true;
        try{
            DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);            
                           Request mRqRequest = new Request(
                                   Uri.parse("http://"+model.getDownloadURL()));
                           long idDownLoad=mManager.enqueue(mRqRequest);
                           DownloadManager.Query query = null;
                           query = new DownloadManager.Query();
                           Cursor c = null;
                             if(query!=null) {
                                        query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                                                DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);                                         
                             } else {
                        return flag;
                            }
                             c = mManager.query(query);
                                if(c.moveToFirst()) { 
            int status =c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 


                               while (downloading)
                               {    Log.i ("FLAG","Downloading");
                                   if (status==DownloadManager.STATUS_SUCCESSFUL)
                                   {    Log.i ("FLAG","done");
                                       downloading = false;
                                       flag=true;
                                       break;      
                                   }
                                   if (status==DownloadManager.STATUS_FAILED)
                                   {Log.i ("FLAG","Fail");
                                       downloading = false;
                                       flag=false;
                                      break;
                                   }
                       c.moveToFirst();
                               }
        }
                            return flag;
        }
        catch (Exception e)
        {
             flag = false;
                return flag;
        }    
    }

但是DownloadManager状态从不在DownloadManager.status\u成功或DownloadManager.status\u失败时跳转。

您必须重新查询下载管理器。即使数据发生变化,光标也保持不变。试着这样做:

protected Boolean doInBackground(String... f_url) {
    boolean flag = true;
    boolean downloading =true;
    try{
        DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);            
        Request mRqRequest = new Request(
        Uri.parse("http://"+model.getDownloadURL()));
        long idDownLoad=mManager.enqueue(mRqRequest);
        DownloadManager.Query query = null;
        query = new DownloadManager.Query();
        Cursor c = null;
        if(query!=null) {
            query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);                                         
        } else {
            return flag;
        }

        while (downloading) {
            c = mManager.query(query);
            if(c.moveToFirst()) { 
                Log.i ("FLAG","Downloading");
                int status =c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 

                if (status==DownloadManager.STATUS_SUCCESSFUL) {
                    Log.i ("FLAG","done");
                    downloading = false;
                    flag=true;
                    break;      
                }
                if (status==DownloadManager.STATUS_FAILED) {
                    Log.i ("FLAG","Fail");
                    downloading = false;
                    flag=false;
                    break;
                }
            }
        }

        return flag;
    }catch (Exception e) {
        flag = false;
        return flag;
    }    
}

不需要异步任务或同步查询。DownloadManager已经是异步的。您应该注册一个BroadcastReceiver,以便在下载完成(或失败)时收到通知


下载管理器以异步方式下载文件有一个很好的例子。因此,无需将下载管理器放在Asyntask中

如果下载完成,您可以使用接收器获取下载管理器的状态

    public class CheckDownloadComplete extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

         String action = intent.getAction();
            if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) 
            {
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
                DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                Cursor cursor = manager.query(query);
                if (cursor.moveToFirst()) {
                    if (cursor.getCount() > 0) {

                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        Long download_id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);

                        // status contain Download Status
                        // download_id contain current download reference id

                        if (status == DownloadManager.STATUS_SUCCESSFUL)
                        {
                            String file = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));

                            //file contains downloaded file name

                            // do your stuff here on download success

                        } 
                    }
                }
                cursor.close();
            }   
    }
}
别忘了在清单中添加收件人

  <receiver
        android:name=".CheckDownloadComplete"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>

您好,我知道这是一篇老文章,但我想知道为什么在CheckDownloadComplete中,您会检查动作是否等于动作\u下载\u完成,尽管它已经在清单中了?你能在课堂上取消支票吗?