【问题标题】:How to create an explicit intent如何创建显式意图
【发布时间】:2016-07-21 16:47:07
【问题描述】:

我了解隐式/显式意图的原则,但我无法从以下代码创建显式意图:

       getActivity().startService(new Intent(PowerampAPI.ACTION_API_COMMAND)
        .putExtra(PowerampAPI.COMMAND, PowerampAPI.Commands.OPEN_TO_PLAY)
        .setData(PowerampAPI.ROOT_URI.buildUpon()
                .appendEncodedPath("folder_playlist_entries")
                .appendEncodedPath(playlist_id)
                .appendEncodedPath("files")
                .build()));

运行此代码会出现以下错误:

.IllegalArgumentException:服务意图必须是明确的:意图{ act=com.maxmpz.audioplayer.API_COMMAND dat=content://com.maxmpz.audioplayer.data/folder_playlist_entries/5/files(有额外内容)}

问题:如何将其转化为明确的意图?

【问题讨论】:

  • 以下链接可能对您有所帮助:stackoverflow.com/a/5940841/5372087 您只需添加包含您要调用的应用程序包名称的意图过滤器
  • 感谢 Bhoomit,链接中有很多建议。我将不得不花一些时间来探索它们

标签: android android-intent playlist


【解决方案1】:

我了解到您正在尝试启动一项服务。如果是这种情况,您将必须为 new Intent() 提供两个参数,如下所示:

startService(new Intent(this, service.class));

这样的声明为 startService 方法提供了明确的意图。

您可以进一步参考此链接了解服务以及如何启动服务:

https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

【讨论】:

  • 这是我的问题。我在 developer.android.com 上阅读了所有这些信息,但并不明智。在您的示例中,我提供的代码将给 service.class 什么?
【解决方案2】:

试试这个,它可能会有所帮助。

Intent i = new Intent("com.maxmpz.audioplayer");
i.putExtra(PowerampAPI.COMMAND, PowerampAPI.Commands.OPEN_TO_PLAY);
i.setData(PowerampAPI.ROOT_URI.buildUpon()
                .appendEncodedPath("folder_playlist_entries")
                .appendEncodedPath(playlist_id)
                .appendEncodedPath("files")
                .build());
startService(i);

【讨论】:

  • 谢谢,但没有改变,服务意图必须明确:Intent { act=com.maxmpz.audioplayer dat=content://com.maxmpz.audioplayer.data/folder_playlist_entries/3/files (有额外的)
【解决方案3】:

我发现了一段代码,它可以将隐式意图转化为显式意图。来源:http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html

      Intent intent = new Intent(PowerampAPI.ACTION_API_COMMAND);
        intent.putExtra(PowerampAPI.COMMAND, PowerampAPI.Commands.OPEN_TO_PLAY)
                .setData(PowerampAPI.ROOT_URI.buildUpon()
                .appendEncodedPath("playlists")
                .appendEncodedPath(playlist_id)
                .appendEncodedPath("files")
                .build());
Intent explicit_intent = new Intent(createExplicitFromImplicitIntent(getActivity(), intent));
getActivity().startService(explicit_intent);

      /***
 * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
 * "java.lang.IllegalArgumentException: Service Intent must be explicit"
 *
 * If you are using an implicit intent, and know only 1 target would answer this intent,
 * This method will help you turn the implicit intent into the explicit form.
 *
 * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
 * @param context
 * @param implicitIntent - The original implicit intent
 * @return Explicit Intent created from the implicit original intent
 */
public Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
    // Retrieve all services that can match the given intent
    PackageManager pm = getActivity().getPackageManager();
    List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

    // Make sure only one match was found
    if (resolveInfo == null || resolveInfo.size() != 1) {
        return null;
    }

    // Get component info and create ComponentName
    ResolveInfo serviceInfo = resolveInfo.get(0);
    String packageName = serviceInfo.serviceInfo.packageName;
    String className = serviceInfo.serviceInfo.name;
    ComponentName component = new ComponentName(packageName, className);

    // Create a new intent. Use the old one for extras and such reuse
    Intent explicitIntent = new Intent(implicitIntent);

    // Set the component to be explicit
    explicitIntent.setComponent(component);

    return explicitIntent;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多