Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when the user launches another app? (Android)

I'm trying to build an application where my application runs in the background and detects when the user launches another application so that I can control the flow from thereon. To illustrate my query, I'd like to specify an example. My application is running in the background (say as a Service), and the user has just clicked on application 'XYZ'. Is it possible for my app to detect that app 'XYZ' has been launched? More than just detecting whether 'XYZ's Activity has come to the foreground,I want to detect whther 'XYZ' has been launched or not. Say someone launches 'Whatsapp Messenger', I want to know if my app can know that 'Whatsapp Messenger' has been launched.

EDIT : A lot of people think I'm trying to build malware, but I'm not. I'm trying to build an app for a high school project. I want a stat to see how often I use my camera as part of a psych project. :/

Thanks in advance, Sumit.

like image 307
Sumit Shyamsukha Avatar asked Jul 05 '12 14:07

Sumit Shyamsukha


People also ask

Can an Android app launch another app?

In android, we can lunch other applications using packing name. This example demonstrate about How to Launch an application from another application on Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How does Android know which activity to run first?

Unlike programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.


1 Answers

Yes, You can find the which application is launched, by Tracking the Logcat. Just Track on ActivityManager tag with info -I log.

From adb shell Command is,

adb logcat ActivityManager:I *:S

From your application code,

logcat ActivityManager:I *:S

And in Logcat you can find a line something like,

I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}

When any application will launched.

It is logcat output that shows that the message relates to priority level "I" and tag "ActivityManager":

Update:

Just add permission in your Application's manifest file,

android.permission.READ_LOGS
like image 171
user370305 Avatar answered Sep 16 '22 13:09

user370305