osa*_*gan 38 android android-package-managers android-download-manager android-settings
我正在使用Android下载管理器下载文件列表.最近我遇到了一份崩溃报告说
Unknown java.lang.IllegalArgumentException: Unknown URL content://downloads/my_downloads
然后,我想通了,原因是因为用户禁用了Android下载管理器.我通过使用以下代码检查下载管理器的包名来检查是否已禁用下载管理器.
int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");
Run Code Online (Sandbox Code Playgroud)
现在,我需要找到一种方法来启用下载管理器,如果它被禁用.我尝试使用Manifest中的权限设置它的启用状态但我一直得到安全异常.
this.getPackageManager().setApplicationEnabledSetting("com.android.providers.downloads", PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
Run Code Online (Sandbox Code Playgroud)
所以我认为它可能无法访问,因为它是一个系统应用程序.(谷歌Play应用程序).
有没有办法将用户重定向到下载管理器应用程序信息视图?让用户启用它?如果无法以编程方式在运行时启用它.
Dev*_*ice 22
如果无效,请编辑我的答案
检查下载管理器是否可用:
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);
}
Run Code Online (Sandbox Code Playgroud)
尝试启用下载管理器的解决方案是:
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);
}
Run Code Online (Sandbox Code Playgroud)
osa*_*gan 10
有些人正在寻找这个问题的答案,我才意识到这个问题的答案在某种程度上被删除了.所以我想回答我自己的问题.
无法直接激活/停用Download Manager,因为它是系统应用程序,我们无法访问它.
只有替代方法是将用户重定向到下载管理器应用程序的信息页面.
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);
}
Run Code Online (Sandbox Code Playgroud)
Google Gmail收件箱已检查是否已禁用DownloadManager,如果为true,则显示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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26176 次 |
最近记录: |