DownloadMAnager仅广播DOWNLOAD_COMPLETE

时间:2015-05-14 08:35:56

标签: android broadcastreceiver android-download-manager

为了改善不同组件的通信,我想听听DownloadManager状态。但是,接收者只能获得Download_complete(成功/失败),而我的alson需要状态PEnding,Running,Paused

public class DownloadReceiver extends BroadcastReceiver{

    private Context context;
    private String TAG = "DownloadReceiver";

    @Override
    public void onReceive(Context context, Intent intent){
        Log.i(TAG, "onReceive");
        this.context = context;
        long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        Log.i(TAG, "downloader id: "+downloadId);

        checkDownloadStatus(downloadId);
    }


    private void checkDownloadStatus(long id){
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(
                                                                                        Context.DOWNLOAD_SERVICE
        );
        Cursor cursor = context.getContentResolver()
                               .query(
                                         DownloadStateProvider.CONTENT_URI,
                                         new String[]{DownloadStateTable.COLUMN_ID},
                                         DownloadStateTable.COLUMN_DOWNLOAD_ID + " =? ",
                                         new String[]{"" + id}, null
                               );
        if(cursor.moveToFirst()){
            Log.i(TAG, "record exist");

            // TODO Auto-generated method stub
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(id);
            Cursor dmCursor = downloadManager.query(query);

            if(dmCursor.moveToFirst()){
                Log.i(TAG, "download exist");

                int columnIndex = dmCursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                int status = dmCursor.getInt(columnIndex);
                int columnReason = dmCursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                int reason = dmCursor.getInt(columnReason);
                dmCursor.close();
                String reasonText = "";
                ContentValues values = new ContentValues();
                switch(status){
                    case DownloadManager.STATUS_FAILED:
                        switch(reason){
                            case DownloadManager.ERROR_CANNOT_RESUME:
                                reasonText = "ERROR_CANNOT_RESUME";
                                break;
                            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                                reasonText = "ERROR_DEVICE_NOT_FOUND";
                                break;
                            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                                reasonText = "ERROR_FILE_ALREADY_EXISTS";
                                break;
                            case DownloadManager.ERROR_FILE_ERROR:
                                reasonText = "ERROR_FILE_ERROR";
                                break;
                            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                                reasonText = "ERROR_HTTP_DATA_ERROR";
                                break;
                            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                                reasonText = "ERROR_INSUFFICIENT_SPACE";
                                break;
                            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                                reasonText = "ERROR_TOO_MANY_REDIRECTS";
                                break;
                            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                                reasonText = "ERROR_UNHANDLED_HTTP_CODE";
                                break;
                            case DownloadManager.ERROR_UNKNOWN:
                                reasonText = "ERROR_UNKNOWN";
                                break;
                        }
                        values.put(DownloadStateTable.COLUMN_STATUS,4);
                        values.put(DownloadStateTable.COLUMN_REASON,reasonText);
                        break;
                    case DownloadManager.STATUS_PAUSED:
                        switch(reason){
                            case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                                reasonText = "PAUSED_QUEUED_FOR_WIFI";
                                break;
                            case DownloadManager.PAUSED_UNKNOWN:
                                reasonText = "PAUSED_UNKNOWN";
                                break;
                            case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                                reasonText = "PAUSED_WAITING_FOR_NETWORK";
                                break;
                            case DownloadManager.PAUSED_WAITING_TO_RETRY:
                                reasonText = "PAUSED_WAITING_TO_RETRY";
                                break;
                        }
                        values.put(DownloadStateTable.COLUMN_STATUS, 2);
                        values.put(DownloadStateTable.COLUMN_REASON, reasonText);
                        break;
                    case DownloadManager.STATUS_PENDING:
                        values.put(DownloadStateTable.COLUMN_STATUS, 0);
                        values.put(DownloadStateTable.COLUMN_REASON, reasonText);
                        break;
                    case DownloadManager.STATUS_RUNNING:
                        values.put(DownloadStateTable.COLUMN_STATUS, 1);
                        values.put(DownloadStateTable.COLUMN_REASON, reasonText);
                        break;
                    case DownloadManager.STATUS_SUCCESSFUL:
                        values.put(DownloadStateTable.COLUMN_STATUS, 3);

                        values.put(DownloadStateTable.COLUMN_REASON, reasonText);
                        break;
                }
                Log.i(TAG, "status: "+status+", reason: "+reason);

                context.getContentResolver().update(DownloadStateProvider.CONTENT_URI,values,null,null);
            }else Log.i(TAG, "download exist");

        } else Toast.makeText(context, "Saved content error\nQuery is  Empty", Toast.LENGTH_SHORT)
                    .show();
        cursor.close();
    }



}



downloadReceiver = new DownloadReceiver();
registerReceiver(downloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

0 个答案:

没有答案