How to Enable Android Download Manager
Asked Answered
W

4

38

I'm using Android Download Manager to download list of files. Lately I came across a crash report saying

Unknown java.lang.IllegalArgumentException: Unknown URL content://downloads/my_downloads

Then later, I figured it out that the reason is because user disabled Android Download Manager. I check if the Download Manager is disabled by checking it's package name with the code below.

int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");

And now, I need to find a way to enable the Download Manager if it is disabled. I tried setting it's enable state with the permission in Manifest but I keep getting Security Exception.

this.getPackageManager().setApplicationEnabledSetting("com.android.providers.downloads", PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);

<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>

So I thought it might not be reachable because of it is a system app. (Google Play App does it).

Is there any way to redirect the user to the Download Manager Application Info view ? to let the user enables it ? If there is no way to enable it on run time programmatically.

Wade answered 4/2, 2014 at 11:42 Comment(7)
have you got an answer?Circinus
@johnsmith unfortunately no. I think the application you develop needs to be a system application to access the Download Manager settings directly.Wade
Is that you want? [Show app info][1] [1]: https://mcmap.net/q/18412/-how-can-i-start-android-application-info-screen-programmaticallyPignut
I might have tried this one already, but I will give it a try and let you know if it can be a solution. Thanks.Wade
how can you check if download manager is enabled? Please show me an exampleAcrimonious
int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads"); please exlpain me how to use this intAcrimonious
@Acrimonious it should return you one of the value of these integer constants. PackageManager.COMPONENT_ENABLED_STATE_...Wade
W
10

Some folks were looking for an answer to this question and I just realized that the Answer made to this question is somehow deleted. So I want to answer my own question.

There is no way to activate/deactivate Download Manager directly, since it's system application and we don't have access to it.

Only alternative left is to redirect the User to the Info Page of Download Manager Application.

try {
     //Open the specific App Info page:
     Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
     intent.setData(Uri.parse("package:" + "com.android.providers.downloads"));
     startActivity(intent);

} catch ( ActivityNotFoundException e ) {
     e.printStackTrace();

     //Open the generic Apps page:
     Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
     startActivity(intent);
}
Wade answered 5/5, 2015 at 3:6 Comment(0)
A
22

Please edit my answer if is not valid

Check if download manager is available:

   int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");

if(state==PackageManager.COMPONENT_ENABLED_STATE_DISABLED||
state==PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
||state==PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED){

// Cannot download using download manager
}

            else {
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
                request.setDescription(fileName);   
                manager.enqueue(request); 
            }

And the solution for trying to enable download manager is:

packageName = "com.android.providers.downloads"

try {
    //Open the specific App Info page:
    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.setData(Uri.parse("package:" + packageName));
    startActivity(intent);

} catch ( ActivityNotFoundException e ) {
    //e.printStackTrace();

    //Open the generic Apps page:
    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
    startActivity(intent);

}
Acrimonious answered 19/8, 2014 at 17:12 Comment(3)
Thanks for the Answer but the same answer has already been given by @ray_pixar.Wade
i know my friend but i think that everyone should see the solution using codeAcrimonious
And there is nothing to do with download manager itself, as you indicated with "request" and "manager" objects.Wade
W
10

Some folks were looking for an answer to this question and I just realized that the Answer made to this question is somehow deleted. So I want to answer my own question.

There is no way to activate/deactivate Download Manager directly, since it's system application and we don't have access to it.

Only alternative left is to redirect the User to the Info Page of Download Manager Application.

try {
     //Open the specific App Info page:
     Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
     intent.setData(Uri.parse("package:" + "com.android.providers.downloads"));
     startActivity(intent);

} catch ( ActivityNotFoundException e ) {
     e.printStackTrace();

     //Open the generic Apps page:
     Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
     startActivity(intent);
}
Wade answered 5/5, 2015 at 3:6 Comment(0)
S
8

Google Gmail Inbox has check whether the DownloadManager is been disabled,if true then show an AlertDialog to tell user to enable the DownloadManager in Settings.The screenshot show below:

enter image description here

I wrote a class called DownloadManagerResolver to fix this,hope this can help your.:)

public final class DownloadManagerResolver {

private static final String DOWNLOAD_MANAGER_PACKAGE_NAME = "com.android.providers.downloads";

/**
 * Resolve whether the DownloadManager is enable in current devices.
 *
 * @return true if DownloadManager is enable,false otherwise.
 */
public static boolean resolve(Context context) {
    boolean enable = resolveEnable(context);
    if (!enable) {
        AlertDialog alertDialog = createDialog(context);
        alertDialog.show();
    }
    return enable;
}

/**
 * Resolve whether the DownloadManager is enable in current devices.
 *
 * @param context
 * @return true if DownloadManager is enable,false otherwise.
 */
private static boolean resolveEnable(Context context) {
    int state = context.getPackageManager()
            .getApplicationEnabledSetting(DOWNLOAD_MANAGER_PACKAGE_NAME);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return !(state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED ||
                state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
                || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED);
    } else {
        return !(state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED ||
                state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
    }
}

private static AlertDialog createDialog(final Context context) {
    AppCompatTextView messageTextView = new AppCompatTextView(context);
    messageTextView.setTextSize(16f);
    messageTextView.setText("DownloadManager is disabled. Please enable it.");
    return new AlertDialog.Builder(context)
            .setView(messageTextView, 50, 30, 50, 30)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    enableDownloadManager(context);
                }
            })
            .setCancelable(false)
            .create();
}

/**
 * Start activity to Settings to enable DownloadManager.
 */
private static void enableDownloadManager(Context context) {
    try {
        //Open the specific App Info page:
        Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + DOWNLOAD_MANAGER_PACKAGE_NAME));
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();

        //Open the generic Apps page:
        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
        context.startActivity(intent);
    }
}
}
Stolzer answered 8/9, 2015 at 10:14 Comment(1)
Thanks for the answer. But please read the accepted answer and tell me if there is anything different you are providing with your answer. It's exactly the same answer, but wrapped with a Dialog Window.Wade
I
0

May be it's help to you.

downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
   DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
   //Restrict the types of networks over which this download may proceed.
   request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
   //Set whether this download may proceed over a roaming connection.
   request.setAllowedOverRoaming(false);
   //Set the title of this download, to be displayed in notifications (if enabled).
   request.setTitle("My Data Download");
   //Set a description of this download, to be displayed in notifications (if enabled)
   request.setDescription("Android Data download using DownloadManager.");
   //Set the local destination for the downloaded file to a path within the application's external files directory
   request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"CountryList.json");

   //Enqueue a new download and same the referenceId
   downloadReference = downloadManager.enqueue(request);

http://www.mysamplecode.com/2012/09/android-downloadmanager-example.html

Ilion answered 21/7, 2014 at 9:23 Comment(3)
This answer has nothing to do with the question. This code just queues the requests to download manager. What I need to do is to enable Download Manager, which is normally getting enabled by user from settings -> applications -> downloadManager -> enable.Wade
It's only possible via system settings. Otherwise it's showing Security Exception. I have tried like as below. <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/> int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads"); this.getPackageManager().setApplicationEnabledSetting("com.android.providers.downloads", PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);Ilion
So you need to be system application to make it enabled. Which is not the case and I guess it's not possible with a regular set of permissions. Thanks for the contribution.Wade

© 2022 - 2024 — McMap. All rights reserved.