如何检测用户何时启动另一个应用程序?(安卓) [英] How to detect when the user launches another app? (Android)

查看:24
本文介绍了如何检测用户何时启动另一个应用程序?(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个应用程序,我的应用程序在后台运行并检测用户何时启动另一个应用程序,以便我可以从那里控制流程.为了说明我的查询,我想指定一个示例.我的应用程序在后台运行(比如作为服务),用户刚刚点击了应用程序XYZ".我的应用程序是否可以检测到应用程序XYZ"已启动?不仅仅是检测 'XYZ's Activity 是否出现在前台,我想检测 'XYZ' 是否已经启动.假设有人启动了Whatsapp Messenger",我想知道我的应用是否可以知道Whatsapp Messenger"已启动.

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. :/

提前致谢,顶.

推荐答案

是的,您可以通过跟踪 Logcat 找到启动了哪个应用程序.只需使用 info -I 日志跟踪 ActivityManager 标记.

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

来自 adb shell 命令是,

From adb shell Command is,

adb logcat ActivityManager:I *:S

从您的应用程序代码,

logcat ActivityManager:I *:S

在 Logcat 中你可以找到类似的一行,

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.

logcat 输出显示消息与优先级I"和标签ActivityManager"相关:

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

更新:

只需在应用程序的清单文件中添加权限,

Just add permission in your Application's manifest file,

android.permission.READ_LOGS

这篇关于如何检测用户何时启动另一个应用程序?(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆