How to call a service from another application? - android

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().

Related

How to startActivity from service that has android:singleUser and permission INTERACT_ACROSS_USERS

I'm looking to expand my app to handle Guest Mode, introduced in Android L. I found that if I create a service with android:singleUser in AndroidManifest, with permission INTERACT_ACROSS_USERS, and I'm a system app by installing it in /system/priv-app, then my service is running even as I switch user. But my app needs to interact with the user, by being able to launch an activity, show a toast or notification. All of those things seems to not be possible. Is there a particular flag I need to set when I call startActivity so that it will launch a new activity from my service?
I found a way to do it. Basically have a singleton Service, which is a service with the android:singleUser="true" and with INTERACT_ACROSS_USERS and have the APK installed in /system/priv-app. Then have it broadcastAsUser to all users. You'll need to use reflection to access methods in UserManager. Then have a receiver instance which will receive the broadcast in the guest user's space, and then have the receiver startActivity.
There are several internal apis (comments as #hide) like Context.startActivityAsUser, NotificationManager.notifyAsUser to support it, but it needs build from source also with platform signature.

how to prevent other app to bind to my service

I can find the way to setup Bound Services in android AIP Guides!
This demo allows any app to bind to it.But what I want is only my apk can communicate with the service I code.Is there any way to do it? How to?
Thanks!
Services are not exported by default, meaning they can only be called/bound by your application. Unless you add android:exported="true" to your service's manifest entry, only your app will be able to bind to the service.
Service.onBind() receives intent that was used to bind to your service. One approach is to add an permission extra to the aforementioned intent, check for its value in Service.onBind(), and deny any bind requests in case of invalid permission value.

Android application as a service without activity

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

Android call method from another app without starting it

I have two android applications with different packages App1 and App2. Suppose I want to call a method Method1 written in App1, from App2. One solution I found in the following link, Android call method from another app, suggested that we should register a BroadcastReceiver in App1 and call sendBroadcast() from App2. But the problem is, I could call the Method1 only if App1 is running in the background. Otherwise, nothing is happening.
How to resolve this issue? Are there any other ways to call Method1 without having to start App1?
But the problem is, I could call the Method1 only if App1 is running in the background.
This is incorrect, if you register any component (BroadcastReceiver, Service, Activity, etc.) in the AndroidManifest.xml and it is exported, other applications can trigger it with an Intent regardless of the current state of the application process.
Perhaps the issue you are running into is that the example you linked to registers the BroadcastReceiver in Java code. If you instead publish the <receiver> in your manifest, it will be externally accessible always. This is explained in the SDK Documentation for BroadcastReceiver.

Always declare Activities and Services in the AndroidManifest file?

I know you must declare all your Activities and Services in the AndroidManifest.xml file in order to make them accessible to the system (as said in the official docs) but if I have a Service or an Activity that I want to be started only by another Activity in my app, do I need to declare it in the manifest? I.e., I always launch a secondary activity from the primary activity of my app that directly points the secondary activity's class (no intent filter resolution), is still necessary to declare the secondary activity in the manifest? And what if I don't want anyone outside my app to be able to launch my secondary activity? I'm sorry if it is a stupid question, I just want to understand if it is a good practice (if possible) to omit activities and services from the manifest file when you want them to be started only by pointing their respective classes within the same app.
You must declare all of your activities and services (and everything else like BroadcastReceivers) in the AndroidManifest.xml file.
You won't be able to use them otherwise.
EDIT: As per CommonsWare comment, adding android:exported="false" to the AndroidManifest.xml's declaration of the activity would prevent your secondary activity from being launched outside of your application.

Categories

Resources