i have two projects in Eclipse: service and UI client.
In onCreate in UI client I have:
startService(new Intent(this, ExampleService.class));
but this fragment:
ExampleService.class
t开发者_JS百科hrows NoClassDefFoundError exception at runtime. I have installed ExampleService.apk, and ExampleUiClient.apk. Project compiles, and everything looks fine. What I am doing wrong?
Is it possible to start service from other apk?
If you're trying to use a service from a different package, you can't use the simple invocation in the Android example because your first argument references the context of your current package, which is by definition the wrong place to look.
You have to create the intent and then use setClassName(String, String), where the arguments are the other package and the full name of the service. For example:
Intent intent = new Intent();
intent.setClassName("com.example.Something", "com.example.Something.MyService");
startService(intent);
Also note this is a simple case which presumes a default action with no arguments - you also have to make sure that the package containing the service has android:exported="true" set for the service if you don't have any intent-filter declarations, otherwise the call will fail due to a lack of permissions.
Use a broadcast/broadcast receiver
The service needs to be declared in the AndroidManifest.xml of your client.
精彩评论