I'm trying to develop an app that prevents a user from getting to a specified app without a password. The scenario is...
- user clicks on "Email" app (for example)
- my app detects launch of an app
- my app confirms it is the "Email" app
- my app opens a view over the top, asking for a password
- user enters a password, if correct, my app disappears, leaving the "Email" app on top
I'm ok doing the rest of it, just part 2 is puzzling me, and after many days reading up on Broadcast Intents etc and trying to listen for "android.intent.action.MAIN" etc in my trial projects I can't seem to detect when an app other than mine is started.
Can anyone help? Am I going about it the right way, in looking for new apps broadcasting an intent to start, or should I be reading the system log for new intents, or doing something in native code?
Any pointers would help, even if you can't answer it fully I'll be able to do some more research. Thanks a lot. Ian
getRunningTasks()
is deprecated in Android L.To obtain app usage statistics you can use UsageStats class from android.app.usage package.
The new App usage statistics API allows app developers to collect statistics related to usage of the applications. This API provides more detailed usage information than the deprecated getRecentTasks() method.
To use this API, you must first declare the
android.permission.PACKAGE_USAGE_STATS
permission in your manifest. The user must also enable access for this app throughSettings > Security > Apps with usage access
.Here is a basic app example showing how to use App usage statistics API to let users collect statistics related to usage of the applications.
A gimmicky way to do it is have a service with a timed loop that checks
You run through that list to look at what is running on the phone. Now you can identify them with ids and processName, so for standard activity this is easy for custom ones well unless you stop them all its hard to discriminate...
Note: this isnt a list of whats is actually on the screen, just a list of whats is running...kinda nullifying your goal maybe but at least you will know when something is starting to run... it will keep being in that list even when in background though.
For the password thing you can just start your activity when you found an app thats protected or whatever.
I think and hope this is not possible. Consider how easily such functionality could be abused by malicious software. You can listen to intents directed at you, and those that are broadcast, but application launching should not be a broadcast event.
What you may be able to do is replace the launcher. If the user agrees to it.
You can get current running
Activity
and check if thisActivity
corresponds toEmail
application.Run
CheckRunningActivity
Thread
onApplication
start (or on device boot).Update: This class need
android.permission.GET_TASKS
permission, so add next line to the Manifest:I think we can use
logcat
and analyze it's output.In all similar programs I have found this permission :
android.permission.READ_LOGS
It means all of them use it but it seems the program starts and after that our program (app protector) will start and bring front.
Use below code :
And do not forget to add it's permission in Manifest file.
The main issue is you are trying to listen for implicit intents when the Launcher (home screen) is typically using explicit intents.
An implicit intent is when you want to say "Somebody play this video" and Android picks an app that can handle that intent.
An explicit intent is what happens when you click the "Email" icon on the home screen. It is specifically telling Android to open that specific app by fully qualified name (i.e. com.android.mail or something).
There is no way AFAIK to intercept such explicit intents. It is a security measure built into Android that no two Activities can have the same fully qualified package name. This prevents a third party from cloning the app and masquerading as that app. If what you wish to do was possible, you could theoretically install an app that could block all of your competition's apps from working.
What you are trying to do goes against the Android security model.
One thing you could do is partner with specific app developers to forward the intents to your security system, but that's probably not something you want to deal with.