Question

I read the file from the SD card , and show on the Gridview.

When I select the item from Gridview and get the position of item.

I click the download button it will download the item.

How to Stop the item download when I using the downloadManager ?

The code is download button is like the following:

FileNode file = mFileList.get(temp_position) ;//Get the item I have select from Gridview

                                final String filename = file.mName.substring(file.mName.lastIndexOf("/") + 1) ;
                                final String urlString = "http://" + mIp + file.mName ;

                                String serviceString = Context.DOWNLOAD_SERVICE ;
                                DownloadManager downloadManager ;
                                downloadManager = (DownloadManager) getActivity().getSystemService(
                                        serviceString) ;

                                Uri uri = Uri.parse(urlString) ;
                                DownloadManager.Request request = new Request(uri) ;
                                request.setTitle(filename) ;
                                request.setDescription(urlString) ;

                                String ext = filename.substring(filename.lastIndexOf(".") + 1)
                                        .toLowerCase(Locale.US) ;
                                String mimeType = MimeTypeMap.getSingleton()
                                        .getMimeTypeFromExtension(ext) ;

                                Log.i("MIME", ext + "  ==>  " + mimeType) ;

                                if (mimeType != null) {
                                    request.setMimeType(mimeType) ;
                                }
                                request.allowScanningByMediaScanner() ;

                                request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) ;

                                request.setDestinationInExternalPublicDir(MainActivity.sAppName, filename) ;

                                downloadManager.enqueue(request) ;

How to Stop the item download when the file is downloading ?

Was it helpful?

Solution

DownloadManager#enqueue returns a long representing the id of the download taking place. Save that long in a variable.

Then, if you need to cancel the download, call DownloadManager#remove() passing in that long.

Eg

//start a download
long id = downloadManager.enqueue(request);

//stop a download
downloadManager.remove(id);

OTHER TIPS

first of all save the long id of download request in shared preferences

like this

SharedPreferences preferenceManager = PreferenceManager.getDefaultSharedPreferences(ACTIVITY CONTEXT);
    Editor PrefEdit = preferenceManager.edit();
    PrefEdit.putLong("Download_ID", id);
    PrefEdit.commit();

now write a custom broadcast receiver which will listen to click on recently started download.

    public class DownloadManagerBR extends BroadcastReceiver {
        DownloadManager down_m ;
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            down_m = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
            SharedPreferences preferenceManager 
            = PreferenceManager.getDefaultSharedPreferences(context);
            long id = preferenceManager.getLong("Download_ID", 0);
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

            }
            else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
            //IN THIS SECTION YOU CAN WRITE YOUR LOGIC TO CANCEL DOWNLOAD AS STATED IN ABOVE ANSWER 
              downloadManager.remove(id);
            }
        }
    } 

and register your broadcast receiver in Android Manifest like this

<receiver android:name=".DownloadManagerBR" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
            </intent-filter>
        </receiver>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top