9

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.

Sumit Shyamsukha
  • 308
  • 2
  • 3
  • 8

4 Answers4

3

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
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 2
    Fair Warning: this method is not bulletproof. Some of the newer phones (I've witnessed on GS3) no longer put the package / activity into this log statement. If you are unconcerned with preformance you can however keep polling the ActivityManager.getRecentTasts() to know when and what gets launched. – FoamyGuy Jul 05 '12 at 15:55
  • will not work from JellyBean+. READ_LOGS permission is now reserved for system apps only.? – Muhammad Riyaz Aug 04 '16 at 08:56
1

I guess you should have a look at "app protector" applications in the Google Play. They detect that user launched another application. That is done by reading system logs. Try opening LogCat and reading logs after you launched any application on device. You'll be surprised.

And where should you go from there? I guess you should try aLogCat app. It's freen and open-source. That will help you to actually read logs.

All this is considered to be a security breach in Android by some developers, though.

1

I have made a service which can detect if other application launches. I have made it for dialer. similarly that can be replaced by any package name.

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    Toast.makeText(this,"Service Started", Toast.LENGTH_LONG).show();

    final String str = "";
    Timer timer  =  new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        int phonelaunched = 0,phoneclosed =0;
        int phonelaunches = 1;
        @Override
        public void run() {
            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

            for ( ActivityManager.RunningAppProcessInfo appProcess: runningAppProcessInfo ) {
                Log.d(appProcess.processName.toString(),"is running");
                if (appProcess.processName.equals("com.android.dialer")) {
                    if ( appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND  /*isForeground(getApplicationContext(),runningAppProcessInfo.get(i).processName)*/){
                        if (phonelaunched == 0 ){
                            phonelaunched = 1;
                            Log.d(str,"dude phone has been launched");
                        }
                        else if (phoneclosed == 1){
                            phonelaunches++;
                            phoneclosed = 0;
                            Log.d(String.valueOf(phonelaunches),"dude that was counter");
                        }
                    }
                    else {
                        phoneclosed = 1;
                        Log.d(str,"dude phone has been closed");

                    }
                }
            }
        }
    },2000,3000);

    return START_STICKY;
}

Here I go through all the running tasks and check if it is our intended application. If so I check if the application is foreground and application is never launched using 'phonelaunched' variable. phoneclosed is used when intended application is in background and variable is set accordingly.

All this is implemented in Service class

forcode
  • 25
  • 5
  • 1
    This code will not run at android L anymore. Use `android.app.usage` package instead. https://developer.android.com/reference/android/app/usage/package-summary.html – Plo_Koon Sep 06 '15 at 13:33
  • But I had to write it for devices running below lollipop. Can we directly use android.app.usage in below lollipop devices?? – forcode Sep 06 '15 at 16:54
-1

In my book, by the way you posed the question, that sounds like hi-jacking an app in a certain way for your service to control, bordering on malware jinx. But it will not work in Android - plain and simple due to the permissions of each application is different. Thereby, each app are isolated from one another. So to answer your question bluntly, its No.

As the other answer suggested - you can monitor the logcat but.. then again... why?

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • As far as this is part of public API, I see no reasons not to do it. –  Jul 05 '12 at 14:48
  • @Mighter Ask the OP why he wants to control an app from a service? Sure its part of public API and "you see no reasons not to do it", right, if I ever found out that app was doing that - one word - *uninstall*. – t0mm13b Jul 05 '12 at 14:49
  • Sure he wants to build a malware. –  Jul 05 '12 at 14:50
  • As I said, I'm trying to build an app for a high school project. I'm not trying to build any malware. :| – Sumit Shyamsukha Jul 05 '12 at 14:56