您的位置:首页 > 其它

内部的service报错(适用broadcast)

2016-12-20 15:31 323 查看
java.lang.InstantiationException:
class has no zero argument constructor

我为了共享定位信息把locationservice放到activity

首先是遇到了找不到service.class的问题Intent里面,mainifest文件声明service也报错,后来发现是需要声明为public。所以我就想非显式调用

5.0以上不允许非显式调用的问题
参考 http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 查了网上解决方案
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;
}
转化
Intent
serviceIt
=
new
Intent(createExplicitFromImplicitIntent(this,tmpIntent));
startService(serviceIt);

最后解决了非显式问题后启动应用又报错:
java.lang.InstantiationException: class has no zero argument constructor
我看字面意思是没有无参构造函数
所以我自己添加了无参构造函数
结果还是报相同错
后来上网查资料发现 http://stackoverflow.com/questions/29947038/java-lang-instantiationexception-class-has-no-zero-argument-constructor inner Broadcast receiver must be static ( to be registered through Manifest)
所以我把这个locationService声明为static就好了

前后调试找资料花费了挺多时间,记录一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: