Kiran Kiran - 5 months ago 24
Android Question

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
?

Answer

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.

Comments