简体   繁体   中英

Android DownloadManager

I am using Android DownloadManager to download some file say of XMB if DownloadManager completes download it will send broadcast of action android.intent.action.DOWNLOAD_COMPLETE in normal scenario.

My question is what if the internet connection gets lost in between. Will it send any broadcast? Same case in between if server stops serving what DownloadManager does. Maybe its silly question, I have very small file so I am unable to test this scenario.

Could some one tell me what DownloadManager does in these kind of scenarios?

The broadcast will be sent. You need to check the status of DownloadManager to determine if it was successful. For example:

private DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            Query query = new Query();
            query.setFilterById(enqueue);
            Cursor c = dm.query(query);
            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                    // handle data
                } else if (DownloadManager.STATUS_FAILED == c.getInt(columnIndex)) {
                    // handle error
                }
            }
        }
    }
};

For a full example, see: this link . For ways to determine the reason of the failure, see this link .

Courtesy : Android DownloadManager Example

I register my receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast only. This is invoked only when the download is successfully completed

Follow this three steps

Create a BroadcastReceiver as shown in snippet below.Inside the receiver we just check if the received broadcast is for our download by matching the received download id with our enqueued download.

private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           //Fetching the download id received with the broadcast
           long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
           //Checking if the received broadcast is for our enqueued download by matching download id
           if (downloadID == id) {
               Toast.makeText(MainActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();
           }
       }
   };

Once the BroadcastReceiver is created you can register for ACTION_DOWNLOAD_COMPLETE in the onCreate method of your activity.

@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       registerReceiver(onDownloadComplete,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

   }

It is also important that you unregister the BroadcastReceiver in onDestroy. This ensures you only listen for this broadcast as long as the activity is active

@Override
  public void onDestroy() {
      super.onDestroy();
      unregisterReceiver(onDownloadComplete);
  }

Complete example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM