简体   繁体   English

如何检测用户何时启动另一个应用程序? (Android)

[英]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'. 我的应用程序在后台运行(例如作为服务运行),并且用户刚刚单击了应用程序“ XYZ”。 Is it possible for my app to detect that app 'XYZ' has been launched? 我的应用程序是否有可能检测到应用程序“ XYZ”已启动? More than just detecting whether 'XYZ's Activity has come to the foreground,I want to detect whther 'XYZ' has been launched or not. 我不仅要检测“ XYZ”的活动是否已成为前台,还想检测“ XYZ”是否已启动。 Say someone launches 'Whatsapp Messenger', I want to know if my app can know that 'Whatsapp Messenger' has been launched. 假设有人启动了“ Whatsapp Messenger”,我想知道我的应用程序是否可以知道已经启动了“ Whatsapp Messenger”。

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。

Yes, You can find the which application is launched, by Tracking the Logcat . 是的,您可以通过跟踪Logcat找到启动哪个应用程序。 Just Track on ActivityManager tag with info -I log. 只需跟踪带有信息-I日志的ActivityManager标签即可。

From adb shell Command is, 从adb shell命令开始,

adb logcat ActivityManager:I *:S

From your application code, 根据您的应用程序代码,

logcat ActivityManager:I *:S

And in Logcat you can find a line something like, 在Logcat中,您可以找到类似以下内容的行:

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": logcat输出显示消息与优先级“ I”和标签“ ActivityManager”相关:

Update: 更新:

Just add permission in your Application's manifest file, 只需在应用程序的清单文件中添加权限,

android.permission.READ_LOGS

I guess you should have a look at "app protector" applications in the Google Play. 我想您应该看看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. 在设备上启动任何应用程序后,尝试打开LogCat并读取日志。 You'll be surprised. 您会感到惊讶。

And where should you go from there? 那你应该去哪里? I guess you should try aLogCat app. 我想您应该尝试使用LogCat应用程序。 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. 不过,所有开发人员都将这视为Android中的安全漏洞。

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. 如果是这样,我检查应用程序是否是前台应用程序,并且从未使用“ phonelaunched”变量启动该应用程序。 phoneclosed is used when intended application is in background and variable is set accordingly. 当预期的应用程序处于后台并且相应地设置了变量时,将使用phoneclosed。

All this is implemented in Service class 所有这些都在Service类中实现

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. 在我的书中,按照您提出问题的方式,这听起来像是以某种方式对应用进行劫持,以便您的服务可以控制,从而与恶意软件jinx接壤。 But it will not work in Android - plain and simple due to the permissions of each application is different. 但由于每个应用程序的权限不同,因此无法在Android中正常使用-简单明了。 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? 正如另一个答案所建议的那样-您可以监视logcat,但是..再来一次...为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM