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.
Related
I have two apps - App1 and App2. I have started App1 and I want to attach files from App2, which is already started in the background with its MainActivity (responsible for sharing the files).
When App2 is already in the background and I start MainActivity from App1, I do not get the files. However, if App2 wasn't started and is freshly started form App1 - it works fine. I also noticed that when App2 is in the background, the methods used for file sharing are called twice. I think that this is because I have two instances of MainActivity in two different tasks.
To avoid this, I am adding a filter to the Intent, that starts App2 Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK - and App2's already running task is brought to front but I do not receive a result in App1. Does anybody have an idea why this is happening and how can I fix it? Thanks!
This is intended behavior - one task can not pass result to another
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.
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
I'm creating an app using the new Android build system, and using a LocalBroadcastManager to fire notifications from an IntentService back to an Activity.
I've defined the following product flavours as part of the build for 3 separate apps;
productFlavours {
london {
packageName 'com.example.lo'
}
dubai {
packageName 'com.example.du'
}
}
When I build the app using either of these product flavours I am not getting any broadcasts through the manager. I've confirmed that both the service and the activity are running in the same process.
A broadcast receiver is bound as required in the Activity#onCreate method of my activity but does not receive any events. I have also confirmed by printing out the process ID of both the activity and service and both are reporting that this is the same process, so this can't be an issue.
As a test I removed the product flavours and built the app with just the single packagename as defined in the app manifest file and now the broadcast manager seems to function as expected. On the intents that I'm sending I am only setting the action.
Do I need to set anything else?
If you look at the source code of LocalBroadcastManager you'll see that if you give your Intent the flag Intent.FLAG_DEBUG_LOG_RESOLUTION (by using Intent.addFlags(int)), it will print in the logcat pretty useful information: you might want to try that with and without the flavours and compare the result to see what goes wrong.
I have org.cmpny.app and org.cmpny.app.additional namespaces in same project; LocalBroadcastManager works fine, nothing is required to be tuned in AndroidManifest (except that LocalBroadcastManager can't be used with ":remote" services, but that's another point)
I have two apps created using Phonegap and, at the moment, they run only on Android devices. I call app2 from app1 using Intent. How can I have a return message (or something like this) from app2 to app1?
Thanks.
Omar
So app2 is somehow launching an intent to open up app1 and you want to pass some information from app2 into app1? Intents have a putExtra method that lets you attach more information to them that you can later read in app1; see this stackoverflow question: How do I create an android Intent that carries data?
Now, getting this to work with Cordova might be a bit more tricky - how are you launching the intent? If it is some sort of phonegap plugin, there is probably a way to use putExtra.