Launch my Activity when any (or selected) Apps are launched - android

I'm developing a Learning Application. In it, I have an Activity where the user can select some applications from a list of all the applications installed on his device.
Now, I'd like to launch my Activity whenever the user launches any of the selected applications from the app list. Basically I'd like to override the selected Activity by my activity. Once the user complete's some task, the user should be returned to the previously clicked Application.
How do I "Capture" this 'Launching other applications' part? BroadcastReceivers? Any example would be highly helpful. I'd be very grateful if anyone points me in the right direction with reference links.
This is very similar to a Lock Apps Application. But in a very badly twisted kind of way.
I know I have to use a background service to monitor the user activity.

You don't intercept arbitrary application launches, if that's what you're after. Doing this silently goes against the Android (or any reasonable) security model.
What you can do is offer an alternative Home screen.
However, if you just have a list view of available applications, nothing stops you from defining custom behaviours within that list activity.

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

Is it safe to use Intent.FLAG_ACTIVITY_MULTIPLE_TASK?

Background
Let's take the next sceneraio:
the user uses an app X which has multiple activities (like the gmail app).
after navigating the app X a bit, he goes to your app.
in your app, you need to start this X app using an intent, to go to a specific activity within it.
now the user goes to this specific activity on the app X.
the user presses the back button, hoping to go back to your app instead of staying on the app X.
another similar scenario:
the user navigates on your app between activities.
your app went to the background (using the home button, for example).
your app shows a notification that once clicked, it will open a specific activity of your app.
the user clicks on the notification and goes to the specific activity of your app.
the user presses the back button, hoping to return to the app that was shown before clicking on the notification, instead of going to the previous activity on your app that was shown the last time it left it.
It seems that the last step on both scenarios isn't the default behavior.
This is why I've searched what is the best combination of flags for this purpose.
The problem
It seems that the only flag that can achieve the behavior i've described is Intent.FLAG_ACTIVITY_MULTIPLE_TASK (together with Intent.FLAG_ACTIVITY_NEW_TASK).
According to the android API, however, this flag isn't recommended for normal use:
Do not use this flag unless you are implementing your own top-level
application launcher.
...
Because the default system does not include
graphical task management, you should not use this flag unless you
provide some way for a user to return back to the tasks you have
launched.
This information seems as confusing as the rest of the descriptions about intents.
There are other intents flags that I've seen, like Intent.FLAG_ACTIVITY_CLEAR_TASK that achieve a similar result, but they have weird behaviors and/or they use high API
The question
Is it safe to use this flag? Are there any good alternatives to it?
What is the danger of using this flag and what is the meaning of the description on the API of using it?
In your first scenario, if your app needs to start an activity of another app, it can just start this activity within the same task as your application. There is no reason to use any special Intent flags for this (you don't need FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_MULTIPLE_TASK). In step 5 of your scenario, the user will return to your application (because BACK just takes him to the previous activity in the current task). This is the standard, default behaviour.
In your second scenario, my first response is "that isn't the standard behaviour". That means that users probably won't expect to be able to go back to the task that they were doing prior to clicking on the notification. However, if you really want to implement this, then I would suggest that you create a special Activity that is launched from the notification and this special Activity should have a different taskAffinity than the rest of the Activities in your application. In this case, when the special Activity is launched from the notification, it will not bring your application's task to the foreground. It will just create a new task containing just the special Activity. When the special Activity is showing, the user can press the BACK key and this will return him to the task that he was working on prior to clicking on your notification.
In general you should NOT use FLAG_ACTIVITY_MULTIPLE_TASK. The main reason is that if you have several tasks containing your application (or parts of it), it is pretty much impossible for the user to return to a specific one. There is no way to provide different launch icons or different application names (for the different tasks)(, so that the user will see multiple tasks in the "recent tasks" list, but will not be able to tell which one is which. You will have a hard time cleaning up what you are doing and you will just make more problems than you can deal with. There are about a million side effects of using this flag and for general applicaitons there is just no need to do it.

Launch one activity from another

i just want to know how can i detect if the user opens an app so an activity of mine launches as well.
For example, the user opens the sms app and right after a kind of lockscreen appears.
You can create a service which will run int the background and you can use this API to determine which activity is visible. That's how many app lock works.
As far as I understand the Android system, it is not possible unless you are making a custom firmware.

tracking user interaction Android

I am new in Android and is developing an app which runs in background as service to collect user activity. till now my app is able to get information about Time_Start, Time_End and Name of other app used by user.
I want to improve my app to be able to count how many interactions(like user tap, touch,...) user make while using other app.
any help?
Thanks!
AFAIK one can't catch touch events/key presses if they are outside your Activity's context or Application's context. I think it goes against the design principle in general ( every app. is sandboxed in its own DVM).
Also, Android has provided mechanism of Intents to track some specific actions and not evry interaction the user has with the mobile . So, I don't think it seems possible .

Force application to front

The question I'm about to ask may seem dangerous for the user, so here's the story before the question:
I'm working in a compagny that tries to sell Galaxy Tabs to schools (children under 10). So, I've been asked to develop an application that starts on boot showing a login screen. The child HAS to log in before he's allowed to use the tablet (just like logging in a computer).
My application starts on boot, shows the login screen, all buttons are blocked (the kid must not be able to use the tablet before the application lets him) except the home button.
fortunately, it seems impossible to block the home button.
I've been trying to rebring the application to front when onpause/onstop is called, this kind of hacks.
So my question is : Can I prevent an user to quit my application until I let him do so?
I know this sounds like a virus, I'm not really happy to be looking for this kind of solution either.
You have to build a custom Home screen, and then set it as the default Home application.
EDIT: see more on this previously asked question (at How can I create a custom home-screen replacement application for Android?). You can just make your authorization the default Home application, then when they log in it forwards to the regular Home screen. If they hit the home button your app gets called, can check if they have logged in and if they have will just send them to the regular home screen.

Categories

Resources