How to create an explicit intent

自闭症网瘾萝莉.ら 提交于 2019-12-13 06:50:20

问题


I understand the principles of implicit/explicit intents but I am unable to create an explicit intent from the following code:

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

Running this code gives the following error:

.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.maxmpz.audioplayer.API_COMMAND dat=content://com.maxmpz.audioplayer.data/folder_playlist_entries/5/files (has extras) }

Question: How to turn this into an explicit intent ?


回答1:


I understand that you are trying to start a service. If that is the case, you will have to provide two arguments for new Intent(), like this:

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

Such a decleration provides a explicit intent to the startService method.

You can furthur refer ato this link about service and how to start a service:

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




回答2:


try this, it may help.

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



回答3:


I found a piece of code which turns the implicit intent into an explicit one. Source: 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);

and

      /***
 * 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;
}


来源:https://stackoverflow.com/questions/38477599/how-to-create-an-explicit-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!