38

ファイルのリストをダウンロードするために Android Download Manager を使用しています。最近、クラッシュレポートに出くわしました

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

その後、ユーザーが Android Download Manager を無効にしたことが原因であることがわかりました。以下のコードでパッケージ名を確認して、ダウンロードマネージャーが無効になっているかどうかを確認します。

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アプリがそれを行います)。

ユーザーを Download Manager Application Info ビューにリダイレクトする方法はありますか? ユーザーが有効にするには?実行時にプログラムで有効にする方法がない場合。

4

4 に答える 4

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

ダウンロードマネージャーを有効にしようとするための解決策は次のとおりです。

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

}
于 2014-08-19T17:12:50.703 に答える
8

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);
    }
}
}
于 2015-09-08T10:14:26.080 に答える