【问题标题】:How to Enable Android Download Manager如何启用 Android 下载管理器
【发布时间】:2014-02-28 08:47:32
【问题描述】:

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

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

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

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

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

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

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

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

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

【问题讨论】:

  • 你有答案了吗?
  • @johnsmith 不幸的是没有。我认为您开发的应用程序需要是系统应用程序才能直接访问下载管理器设置。
  • 这是你想要的吗? [显示应用信息][1] [1]:stackoverflow.com/a/18873867/1784623
  • 我可能已经尝试过这个,但我会试一试,让您知道它是否可以成为解决方案。谢谢。
  • 如何检查下载管理器是否已启用?请给我一个例子

标签: android android-package-managers android-download-manager android-settings


【解决方案1】:

可能对你有帮助。

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

【讨论】:

  • 这个答案与问题无关。此代码只是将请求排队到下载管理器。我需要做的是启用下载管理器,这通常由用户从设置 -> 应用程序 -> 下载管理器 -> 启用。
  • 只能通过系统设置实现。否则它会显示安全异常。我试过如下。 int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads"); this.getPackageManager().setApplicationEnabledSetting("com.android.providers.downloads", PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
  • 因此您需要成为系统应用程序才能启用它。事实并非如此,我想使用常规权限集是不可能的。感谢您的贡献。
【解决方案2】:

如果无效,请编辑我的答案

检查下载管理器是否可用:

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

尝试启用下载管理器的解决方案是:

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

}

【讨论】:

  • 感谢您的回答,但@ray_pixar 已经给出了相同的答案。
  • 我认识我的朋友,但我认为每个人都应该看到使用代码的解决方案
  • 和下载管理器本身没有任何关系,正如你用“request”和“manager”对象指出的那样。
【解决方案3】:

有些人正在寻找这个问题的答案,而我刚刚意识到对这个问题的回答不知何故被删除了。所以我想回答我自己的问题。

无法直接激活/停用下载管理器,因为它是系统应用程序,我们无权访问它。

唯一的选择是将用户重定向到下载管理器应用程序的信息页面。

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

【讨论】:

    【解决方案4】:

    Google Gmail Inbox 已经检查了 DownloadManager 是否被禁用,如果是,则显示一个 AlertDialog 告诉用户在设置中启用 DownloadManager。截图如下:

    我写了一个名为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);
        }
    }
    }
    

    【讨论】:

    • 感谢您的回答。但请阅读接受的答案,并告诉我您提供的答案是否有任何不同。这是完全相同的答案,但用一个对话框窗口包装。
    猜你喜欢
    • 2011-11-29
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 2014-07-30
    • 2012-05-20
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多