如何启用 Android 下载管理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21551538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:44:25  来源:igfitidea点击:

How to Enable Android Download Manager

androidandroid-package-managersandroid-download-managerandroid-settings

提问by osayilgan

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

我正在使用 Android 下载管理器下载文件列表。最近我看到一个崩溃报告说

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

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.

后来,我发现原因是因为用户禁用了Android下载管理器。我通过使用下面的代码检查它的包名称来检查下载管理器是否被禁用。

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.

现在,如果下载管理器被禁用,我需要找到一种方法来启用它。我尝试使用 Manifest 中的权限设置它的启用状态,但我不断收到安全异常。

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).

所以我认为它可能无法访问,因为它是一个系统应用程序。(Google Play 应用程序做到了)。

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.

有没有办法将用户重定向到下载管理器应用程序信息视图?让用户启用它?如果无法以编程方式在运行时启用它。

采纳答案by osayilgan

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);
}

回答by Device

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);

}

回答by Folyd

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:

谷歌 Gmail 收件箱已经检查下载管理器是否被禁用,如果是,则显示一个 AlertDialog 告诉用户在设置中启用下载管理器。截图如下:

enter image description here

在此处输入图片说明

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

我写了一个名为DownloadManagerResolver的类来解决这个问题,希望这可以帮助你。:)

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);
    }
}
}

回答by Karthikeyan

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

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