Android L (API 21) - java.lang.IllegalArgumentException:服务意图必须是显式的 [英] Android L (API 21) - java.lang.IllegalArgumentException: Service Intent must be explicit
问题描述
Android 新版本 - Lollipop" (API 21) 带来了不少变化,但如果您希望将您的应用定位到该 API,它会付出一些代价.
Android new version - "Lollipop" (API 21) brings quite a few changes with it, but it comes with some price if you wish to target your app to that API.
当我们开始让我们的应用程序适应新的 API 时,我们遇到的第一个问题是 IllegalArgumentException: Service Intent must be explicit
When we started adapting our apps to the new API, one of the first problems we've encountered is the IllegalArgumentException: Service Intent must be explicit
如果您遇到了问题,并且您实际上打算以显式方式使用您的 Intent(这意味着在启动服务时您希望恰好命中 1 个服务操作),那么这里有一个快速解决转隐式的方法 -->显式:
If you have encountered the problem, and you are actually intending to use your intent in explicit way (meaning that when starting the service you are expecting to hit exactly 1 service action), here's a quick fix for turning implicit --> explicit:
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.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;
}
应该可以.请随时发表评论以获取有关此新问题的更多见解.
That should do it. Please feel free to comment for additional insights on this new issue.
推荐答案
显式启动服务
intent = new Intent(context, Service.class);
或在隐式意图中显式提供包
intent = new Intent("com.example.intent.ACTION");
intent.setPackage("com.example")
这篇关于Android L (API 21) - java.lang.IllegalArgumentException:服务意图必须是显式的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!