App inside an app

PKlumpp picture PKlumpp · May 26, 2014 · Viewed 16.9k times · Source

Is it possible to run an application from inside another application? What I want to do is write an app which allows you to chose an app to start, and then displays the activities of this app inside a view.

So in landscape mode, it should look something like this:

enter image description here

The idea behind this is:

I want to be able to start and run a third party activity next to my own activity, and I want to be able to create individual makros with my activity that are controlling the third party activity.

Basically, something like this:

  • Start third party activity from inside my app
  • Start makro recording
  • Do something in third party activity
  • Stop makro recording
  • Use makro whenever you wish

So how can I start and control another activity from inside my own activity?

Answer

Manuel Allenspach picture Manuel Allenspach · Jun 2, 2014

Unrooted:
Sadly, what you want to achieve does not seem to be possible without rooting the phone, because you can only interact with other apps via intents. Since developers decide how their apps react on specific intents, creating macros this way is nearly impossible.

With rooted phones:

  1. You may want to create a list of all installed apps, you can use

    getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
    

    to retrieve a list of all installed apps.

  2. If the user now selects an app, launch it via an intent and create a system overlay to get all touch/key events (and let the user stop the macro). You can find a way to do this here. Store the x/y-values of the touch-events.
  3. You can recreate the events using MotionEvent#obtain.
  4. Now comes the part where you need a rooted phone (the permission INJECT_EVENTS). Launch the app and inject the events so your macro gets executed. Samplecode:

    Instrumentation m_Instrumentation = new Instrumentation();
    m_Instrumentation.sendPointerSync(motionEvent);
    

    You can find more information about injecting (also keyevents) here.

  5. If you need help to compile your app, these 2 links will help you: How to compile Android Application with system permissions, Android INJECT_EVENTS permission