架构需求就是这样,Service类定义在一个Jar包插件,通过网络下载或者push到SD卡中,然后要求Service在新进程中启动。运行时出现找不到Service类的错误,然后程序错误崩溃:Causedby:java.lang.ClassNotFoundException:Didn'tfindclassJarService贴个简单代码示例吧主Activity就是loadSD卡上的jar包,反射获取类名,intent启动servicepublicclassMainActivityextendsActionBarActivity{privatefinalStringTAG="MainActivity";@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i(TAG,"onCreate");ClassLoaderdexLoader=newDexClassLoader("/sdcard/jarservicetest.jar",getCacheDir().getAbsolutePath(),null,getClassLoader());//ClassLoaderdexLoader=getClassLoader();try{Log.i(TAG,"try");ClassactivityClass=dexLoader.loadClass("com.example.jarservicetest.JarService");Intentintent=newIntent(this,activityClass);startService(intent);}catch(ClassNotFoundExceptione){Log.i(TAG,"catch");//TODOAuto-generatedcatchblocke.printStackTrace();}}}Service就一空壳publicclassJarServiceextendsService{privatefinalStringTAG="JARSERVICE";@OverridepublicvoidonCreate(){//TODOAuto-generatedmethodstubLog.i(TAG,"onCreate");super.onCreate();}@OverridepublicvoidonDestroy(){//TODOAuto-generatedmethodstubsuper.onDestroy();}@OverridepublicIBinderonBind(Intentarg0){//TODOAuto-generatedmethodstubreturnnull;}}Manifest文件中声明了Service请教达人们,面对这种需求,有啥解决办法?
2 回答
繁花如伊
TA贡献2012条经验 获得超12个赞
检查下jarservicetest.jar文件里包含的是class文件还是dex文件拿sdcard路径的时候,用Environment.getExternalStorageDirectory()
添加回答
举报
0/150
提交
取消