How to detect when the user launches another app?

2019-01-06 15:49发布

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.

4条回答
Ridiculous、
2楼-- · 2019-01-06 16:17

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-06 16:18

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

查看更多
三岁会撩人
4楼-- · 2019-01-06 16:24

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
查看更多
The star\"
5楼-- · 2019-01-06 16:42

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?

查看更多
登录 后发表回答