0

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?

Community
  • 1
  • 1
Kiran
  • 747
  • 2
  • 10
  • 35

1 Answers1

4

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.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Can we call a method defined in another activity without creating the instance of the activity? Because I have written the broadcast receiver in separate class file and when I am unable to trigger the method written in the Activity class. – Kiran Oct 10 '13 at 19:22
  • If an `Activity` is what you want to launch, then launch the `Activity` directly from your `App2`, not a `BroadcastReceiver`...go straight to the source. Call `startActivity()` with a proper `Intent` rather than `sendBroadcast()`. – devunwired Oct 11 '13 at 05:21