Reputation: 8271
This is driving me nuts because the answer is probably staring me in the face. I'm trying to learn to make a simple service in Android using the example in http://marakana.com/forums/android/examples/60.html But where I try to start my service
startService(new Intent(this, MyService.class));
I get "MyService cannot be resolved to a type" at build-time.
The example has this line in the Manifest just after the close of activity and before the close of application . . .
<service android:enabled="true" android:name=".MyService" />
... but I've also tried it without the "." and with a fully qualified package name, i.e., test.bg.MyService, with no improvement.
Any idea why it can't resolve it?
Upvotes: 1
Views: 3087
Reputation: 48871
I'd suggest you read the documentation on Intents and Intent Filters especially the parts about Explicit and Implicit Intents.
What you are trying to do is use an Explicit Intent to start a Service using a class name which doesn't exist in the project that is attempting to start it (as CommonsWare points out).
As for your belief that...
a service is built as a separate project
...that may be possible but very often that isn't the case and even your link in your comment to CommonsWare actually demonstrates the use of a Local Service and to quote from the opening paragraph of that section...
One of the most common uses of a Service is as a secondary component running alongside other parts of an application, in the same process as the rest of the components. All components of an .apk run in the same process unless explicitly stated otherwise, so this is a typical situation.
So basically that's saying that a typical situation is that your Service will be a component of an application containing other components which will make use of that Service.
Sure, you can use a Service in another application but you need to register it in your manifest with an <intent-filter>
entry and an action such as test.bg.ACTION_DO_SOMETHING
and then have an external application start it using an Implicit Intent.
Using the example code you linked in your question isn't going to work.
Upvotes: 1
Reputation: 1006574
If you are getting "MyService cannot be resolved to a type" during the compile, or as an error from Eclipse, then there is no such class in your project.
It has nothing to do with your manifest -- problems stemming from that will not show up until runtime with the current crop of developer tools.
Upvotes: 1