Android: run another app instead of requested - android

I write 2 applications for Android: A and B.
All seems OK, but my task is very specified.
It's not practical, it is learning low-level Android capabilities.
I need simple following: listen a LAUNCH intent of app A in app B or another way if you know.
The result must be: when I press app A icon, app B must started.
Not in onCreate! I know this method very well, but not a single line of application code A should be executed. Just run app B immediately.
I control both apps A and B, I write them. How I can do described?
Very similar Two launchers for a single activity, only I need to redirect alias to another application.

Apps are isolated so luckily you cannot do that otherwise that would be serious security risk.

Related

App inside an app

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:
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?
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:
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.
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.
You can recreate the events using MotionEvent#obtain.
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.
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
It's not possible to start an application in a View, but you can launch an app from within your app:
Intent i = getPackageManager().getLaunchIntentForPackage("com.package.ofapp");
startActivity(i);
//EDIT to your updated question:
After starting the activity from the above code, one way you could start/stop the macro at any time in the new app would be to create a small view overlay on top of the screen.
This overlay would be on top of ALL activities.
Check out the following link: Creating a system overlay window (always on top)
You could write code to start the macro when the View is pressed, and then if the button was pressed once and the user presses it again, stop the macro. This would be in the onTouchEvent() method.
Yes, I think it's possible as a app named floating apps does that (WITHOUT ROOT)
Only using some adb commands
https://play.google.com/store/apps/details?id=com.lwi.android.flapps
Yes its possible if you use Intents. They allow you to move between screens and to launch another different functionality inside the same app. visit coursera for more tutorials on intents

Android: how to know previous app

I have 2 different applications A and B and I want to create a special animation from B to A, that is when A is opened after B was visible. This means that I need to somehow know the previous app after which my app was opened. I can have different scenarios of going from B to A - using Recent Apps (multitasking) button, using Back button, using Home button (application A is a custom home screen). Are there any ideas how to do this? Some functions in ActivityManager might help, but they have comments in documentation saying not to use them for implementing logic and control flow.
Not sure if this will work across different applications, but how about getCallingActivity() or getCallingPackage()?
If that doesn't work, could you pass along some 'extra' data in the bundle when you launch the intent that indicates the launching application?
I managed to figure out how to implement this, its working for me.
I used this answer, but replaced the ActivityManager.getRunningTasks() with ActivityManager.getRecentTasks() supplying RECENT_WITH_EXCLUDED | RECENT_IGNORE_UNAVAILABLE, and took the component name from baseIntent member of the result. The info at index 1 is the one that was running before the current app was opened, irrelevant of the fact how you get back to your app - back button, home button, recents button or opened from another app.
NOTE: This works when your app is started, like in onResume() but doesn't work when your app is closed (when called in onPause()) because the new task is not yet loaded into the activity manager. So if you need to also know to which app are you going then it might be a bit more complex.
NOTE2: Although the documentation tells not to use the API above for any kind of logic and control flow, I saw that the multitasking/recent app's code is doing exactly the same, so in my opinion it should not be as risky as they write it docs.
NOTE3: Don't forget to follow all the steps in the answer I mentioned above, like adding needed permissions, otherwise you will get exceptions. Being part of the system in my case makes it much easier for me.

how can i get to know which app package is startig my activity?

there are many applications which invokes my application in system> I want to know which app invoked my app or activity or service presently ?
Can anybody help me to solve this ?
In the starting activity of your application you can try following code to get the PackageName of the app that invokes your application.
if(getIntent().getPackage()!=null){
String packageName = getIntent().getPackage();
}
You can provide different activities to start your app, each being called by different apps. Or you can monitor user actions after app launch (navigation to screens etc) and determine which application may have launched your app. These two are relatively logical but apart from this you can monitor and look for currently running apps (look here for one possible way to do that) and then maybe determine which application launched your app.
Note that these are just strategies, not a definite answer.

Application that keeps itself on top, and re-launch itself when closed

I need to "lock" a user into an application. The device's sole purpose is to use this application, and so it is not feasible to allow the user to navigate the device for any other reason. What is the best way to make sure that the applications Activitys are always in the foreground, and if not, launch the main Activity?
Now I know this goes against everything about the typical Android application development, but these devices are going to be specifically used for this one application.
What is the best way to determine if any of the applications Activitys are in the foreground, and launch one if not?
What is the best way to make sure that the applications Activitys are always in the foreground, and if not, launch the main Activity?
You don't.
Make your activity be the home screen, and they can't go anywhere. And roll your own firmware, so that the user can't safe-boot the device and remove your home screen.
I will probably use a service which gonna check using a flag as example for a living activity.
Look here for more detail about how to do : http://developer.android.com/guide/topics/fundamentals.html
See the answer to this question: basically you must implement a second app that captures home intents and simply re-launches your primary application if it is ever closed.
In reference to what CommonsWare was saying, you could actually partially brick the device. I've done it on accident where I've bricked the recovery menu, but not the actual regular boot. This is a terrible idea of course and should not be used practically.

Password protect system application android

When a user tries to launch an application I want to suppress that application and then call another application. Example I want the user to authenticate himself before launching a particular system application (settings application etc). The authentication application should pop up every time the user launches the settings application
I know you will have to use broadcast receivers and intents but have no clue how to do it.
Sounds like you should create a "lib" project that have public interfaces that you can use.
Then share them between the apps instead of trying to execute another app?
But what I know this is not possible to actually execute up another app, since this then gives dependency to something that you don't know if it is installed. It must already been started if the intents should work.
Also like the answer before, it could be used for abuse.
Look at this link for more information:
http://mylifewithandroid.blogspot.com/2007/12/playing-with-intents.html
I sort of hope this isn't possible... Launching a different application from the one the user actually clicked? Leaves the door open for abuse.

Categories

Resources