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
Related
We have two apps now, say app1 and app2. We want to start app2 first and then app2 will call context.bindService() to invoke app1 without even requiring app1 to be started first.
We are using the below code to bind the service
context.applicationContext.bindService(intent, ServiceConnection, Context.BIND_AUTO_CREATE)
In the Manifest we are giving the full service package name and also query packages in manifest like the other posts mentioned.
Now this flow has been working perfectly fine on almost all the devices. But only on one device we checked the logcat and it's reporting
unable to start service intent {cmp...} u=0
But the strange thing is on this device, if we start app1 first and then start app2, it is able to bind. If app1 is not started, then we see the error.
Has anyone ever seen this issue before or have any ideas?
Any help will be much appericated!
We have an app with an Activity that can be started in two ways:
From another Activity - always with some extra data filled in
From deep linking
As far as I can see this is always working just fine. We either get the Intent.ACTION_VIEW with a data URI, or we get some string extras.
However, we have a very few instances where action is Intent.ACTION_MAIN and there are no extra data.
The toString() of the Intent is as follows (class name changed):
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10400000 cmp=com.example.OurActivity }
Intent.getExtras() returns null, Intent.getDataString() returns null.
In which cases can this happen? Why is the category for the Activity Intent.CATEGORY_LAUNCHER? How can we get the data needed to show the user the right contents?
launchMode is not specified for the Activity. The only IntentFilter in AndroidManifest.xml is for the deep linking (and not the launcher category).
The issue happens on Android 4-6 on a wide range of devices.
Edit: Forgot to mention the flags:
As the print out suggests the flags for the Intent are FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_BROUGHT_TO_FRONT. I suppose that could be relevant here.
I believe, I nailed it:
There're launchers, like Nova Launcher which allows users to start with any of app's Activities, instead of the normal flow:
For example, you can add a shortcut on your desktop to start Gmail client with Account setup Activity.
And in this case, Activity is being started with empty Extras and technically it becomes a launcher's Activity.
Now that AndroidManifest.xml is manipulated by the build system, it often happens that libraries that you include also add things to the manifest, which I suspect may be happening here.
Although you state that there is only one IntentFilter in the manifest, have you actually checked the installed app to see what its manifest says (rather than relying on what you think you put in your source code)?
Various apps are available in the Play Store to show you the manifest of an installed app - including App Detective (which I wrote).
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.
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.
I have application A and application B.
I launch B with an intent from A in this way:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.applicationB", "com.applicationB.MainActivity");
intent.putExtra(EXTRA_NAME,"name");
startActivity(intent);
When the user closes application A I want application B to close too. App B has JNI and uses exec() command.
I've tried android:sharedUserId but I got this error when app A tries to launch B with the code above:
Error running exec(). Command: [....] Working Directory: null Environment: (not null, it shows all the environment.
Any ideas?
you must use android IPC mechanisms like broadcast receiver
when application A closes it should send a broadcast and application B should register for a broadcast receiver to capture broadcasts from application A
see the documents for more info http://developer.android.com/reference/android/content/BroadcastReceiver.html
If both applications are programmed by yourself, you could consider to add a BroadcastReceiver in app B, that takes care, that all activities get closed. Before app A closes, call that receiver and it's done.
If you are not sure which activity is shown, you could extend the Activity class with a BroadcastReceiver, so that all activities get notified.
If app B is not programmed by you, you could use killbackgroundProcesses(), e.g.
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(myProcessId);
... however, this works only if app B is in fact in background. Additionally this is not the best option to close an app 'cause you do not know what the app is currently doing.
Edit: found my favorite example on that topic :-) check http://www.hrupin.com/2011/10/how-to-finish-all-activities-in-your-android-application-through-simple-call - it shows how to close all activities from within the app. However, it's not a big step doing this from another one ...