Android新版本 - “Lollipop”(API 21)带来了不少变化,但如果您希望将应用定位到该API,则需要付出一些代价。
当我们开始调整我们的应用程序到新API时,我们遇到的首要问题之一是IllegalArgumentException: Service Intent must be explicit
如果您遇到了问题,并且您实际上打算以明确的方式使用您的意图(意味着在启动服务时您希望完全达到1个服务操作),这里有一个快速修复转向隐含 - &gt ;明确的:
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;
}
应该这样做。如有关于此新问题的更多见解,请随时发表评论。
答案 0 :(得分:3)
intent = new Intent(context, Service.class);
intent = new Intent("com.example.intent.ACTION");
intent.setPackage("com.example")