Android application as a service without activity - android

I am making a set of apps and I have pretty much the same background service for all of them.
I'm trying to make an app that has only this Service. so I don't repeat it in all of them, but the thing is don't need any Activity. because there is no UI needed for it, and so the user can't close it except if they stop the Service.
I tried to remove the Activity, but then the app doesn't run or start.
My question is: can I make an app exactly like Google Play Services so other apps can use its Service.
If yes than a snippet or a sample would be very welcome.

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to.
The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher. If there's no activity, you'll have to find another way to start it.
It is easy to do, though. Just fire an intent from one of your other programs, like this:
startService(new Intent("my.service.intent"));
... where the service is registered your manifest, like this:
<service android:name=".SomeService" >
<intent-filter>
<action android:name="my.service.intent"/>
</intent-filter>
You could use that intent to pass Parcelable parameters to the service, and the service can reply by broadcasting intents back.
Of course startService and broadcastIntent are a bit clunky if you really need a complex API between applications and your service. If you need something richer, you will want to look into AIDL and a Bound Service.
Edited to add Intent Filter

Related

How to get all intent filters for application (with root)

I am working on a system application and I need to know programmatically what intents an application is capable of handling. I have seen other questions related to this, but none of them seem to have an answer but also do not seem to be concerned with system privileges.
PackageManager only seems to provide methods to query activities for a given intent. I can not find a way to get intents for a given activity.
For example, if I have an activity which has intent filters defined as such:
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
And I know the activities class name and package name, I would like to find out from the package manager (or any other source) what intents it can handle (in this case, BOOT_COMPLETED and USER_PRESENT).
Good question. I don't think it's possible to find out the intents that an app can handle. Some standard activity actions and broadcast actions are listed at http://developer.android.com/reference/android/content/Intent.html. If you are writing your own app that's having it's own intent filters, then i don't think other apps will know what intents your app can handle because there is no way to publish them or announce them to the world, i believe.
But what is possible is that you can make a call for a particular intent and if that returns null, then you know for sure that there are no apps on the device that can handle that particular intent.
HTH.
So after just asking this question, I found this wonderful answer here:
Android — How to I get a list of all available intent filters?
Which leads to this 5 year old issue
So it doesn't seem like there is any way to do what I want to do at the moment. If it becomes available, I would love to know.
you could try a decompiler, such as "DexDump" (available on GooglePlay), and retrieve the "AndroidManifest.xml" of the application you want to know about.

How to call a service from another application?

I have 2 applications, Application A and Application B. Application A is running a service called "MyService". I want to call this service from Application B. How to achieve this?
The service should have in Manifest exported="true" - that is the default value anyway ....
Also add an <intent-filter> to the service, with your own custom action string. You may put some permissions to this as well so only the app that has this permission can start. But that's optional.
Then you need to pass an intent that Application A can recognize. So you can use new Intent("the_action_you_defined_in_A_Service_manifest") in your call
to startService().

Service cannot be resolved to a type

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?
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.
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.

How to send Intent to: <service android:name="com.some.example.SomeService" android:exported="true" />

Hello and thanks very much in advance for any help.
I'm trying to figure out how to send an Intent to an exported service that resides in another application. The service has no intent-filters or other attributes. Just a name and exported="true".
I know how to do this when the service specifies a unique action. But, in this case, I have nothing but the service name. I know it sounds a bit strange. Why would I want to do this. But, I'm trying to launch the service before launching the main app so that I can observe behavior. I've tried everything I can think of and googled for several days now to now avail.
Thanks very much again for the help.

accessing one app from another app

Can someone explain me how to control one application from another application? I'm running a music player in an app1 using service class. And I want to stop that music player from another app.,i.e app2. But, I'm fallin short o the concept.
Depends what you need to do.
Opening another activity (or sending messages) is by using Intents:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
// ...
startActivity(intent);
Starting service is by using startService()
What you are trying to do can also be done using Intent broadcasts but only if your target app supports and listens to specific actions on the broadcast. You need to see if there is such an ACTION supported.
I'd like to continue this question a bit.
In my case, I'm developing the target app and I need to implement few simple procedure calls for the main app. Basically 'start', 'stop' and 'sendData'. As I wrote, I'm developing the target app so I can support whatever I want. Which would you say is the easiest way to handle.
The whole situation a bit more explained. Main app would like my app to start it's work, and if needed they'll request that I turn myself off and when the main app is closing it would request me to send my data forward.
I'm quite new to android development, so code snippets are preferable. Thank you.

Categories

Resources