如何创建一个明确的意图 [英] How to create an explicit intent
问题描述
我了解隐式/显式意图的原理,但无法通过以下代码创建显式意图:
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:服务意图必须是明确的:意图{ act = com.maxmpz.audioplayer.API_COMMAND dat = content://com.maxmpz.audioplayer.data/folder_playlist_entries/5/files(有其他功能)}
.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 ?
推荐答案
我发现了一段代码,它将隐式意图转换为显式意图.来源: http://blog.android-develop .com/2014/10/android-l-api-21-javalangillegalargumen.html
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);
和
/***
* 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;
}
这篇关于如何创建一个明确的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!