0

Refering to this post I need to ask something else.

I have an application "A" which download and install another app "B".
I want B to "transfer" data to A and then A will use this data to do work.

I know that we can transfer data with intent.
After installing B app with A, Android provide a choice with "Ok" or "Launch" ; my question is :

  • Is that possible to pass data from B to A when we click on "Ok"? (So we stay in A app without launching B)
  • If yes, how? Is that possible to "invisible" launch B? How should I code B to get this comportement?

I know that might be hard to understand, you can try to check my previous draw (here again).

EDIT:

I use this code to launch B installation from A.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString() + "/downloadedfile.apk"));
Intent.setDataAndType(uri, "application/vnd.android.package-archive");
getApplicationContext().startActivity(intent);
Community
  • 1
  • 1
  • a) "Ok" or "Launch" from app B ? b) or package manager/instalator app? .... obviously: if a) then yes, send broadcast to A in onCreate of B activity and close ... if b) not possible at all – Selvin Jun 19 '15 at 13:12
  • I edit, I use this to install B app then you have manual installtion (like all android app, check permissons, accept then install) so then, Android ask you if you want to launch your new app or not. I DO NOT want to launch **B** or not here, i need to stay in **A** activity but retrieve **B** data – Alexandre Baptiste Jun 19 '15 at 13:27
  • since API11 components of B will be not useable if user do not starts one of its Activity ... – Selvin Jun 19 '15 at 13:36
  • then can we launch **B** without user see it? I mean make it "invisible" or something? – Alexandre Baptiste Jun 19 '15 at 13:38
  • yeah, do not use setContentView + google for which theme for "invisible" activity ... do some stuff(like sending broadcast: "B reporting for duty") in onCreate and also call finish() there ...also remeber about PACKAGE_INSTALL/ADDED broadcast ... you can use it ... so even if user do not select launch you can launch main activity from B after it doesn't send broadcast but app is installed(but you need to test) ........ so in onCreate of B check if A is installed if no show install A activity (similar to install B in A) ... – Selvin Jun 19 '15 at 13:44
  • i have hard time to understand, have you some docs or exmple? – Alexandre Baptiste Jun 19 '15 at 14:16

1 Answers1

1

There are many ways to handle this, here is one that (I believe) is quite simple to implement. Since your A app [presumably] knows what it is installing:

App A: Add a BroadcastReceiver to react to the installation, though by default it is off.

Android: BroadcastReceiver on application install / uninstall

App B: Add a Service for background communication.

Note: A Service must be exported to be accessible to other apps via explicit intent, but this creates a security concern, as it is open to all other apps.

When a user of App A clicks to install App B:

Start the BroadcastReceiver with a filter set to detect the install: stackoverflow...android-broadcastreceiver-on-application-install-uninstall

App A starts the install.

When the BroadcastReceiver detects the package has been added (the package name will be in the received intent,) it can stop the BroadcastReceiver, and can send an explicit Intent naming the Service in AppB. You can pass whatever data you need in the intent.

When the AppB service receives the intent, it can act in any way you'd like.

Service is always created using a non-null Intent, though the 'action' of explicit Intents is null. Service.onStartCommand() might receive a null Intent if the service was re-created.

I'd fill in more of the code, but I have a day job ;)

Note:
Intent.ACTION_PACKAGE_ADDED called when a package is installed.
Intent.ACTION_PACKAGE_INSTALL was never used, and was deprecated in API 14.

http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/content/Intent.html

Community
  • 1
  • 1
CodeShane
  • 6,480
  • 1
  • 18
  • 24
  • Thanks a lot for that. I'll check it as soon as i can (probably next monday) then maybe ask you some others questions ! – Alexandre Baptiste Jun 19 '15 at 14:46
  • After installing my app, i got my BroadcastReceiver. So, I need to call the service in AppB? How can I do that? Is that even possible considering the fact that i didn't even launch AppB? And if it's possible, i'll need to transfer data from AppB service to AppA? – Alexandre Baptiste Jun 22 '15 at 12:54
  • Send an explicit intent (added a link above) from AppA to AppB asking for the data, then one from AppB to AppA with the data. There are plenty of alternative ways to accomplish this, but this is probably the easiest to implement. – CodeShane Jun 22 '15 at 14:52