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

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

问题描述

我试图建立在我的应用程序在后台运行,当用户启动另一个应用程序,这样我可以控制其流量检测的应用程序。 为了说明我的查询,我想说明的一个例子。 我的应用程序在后台运行(比如服务),且用户只需点击应用程序XYZ。是否有可能为我的应用程序,以检测该应用程序XYZ已经启动? 以上只是检测是否XYZ的活动已经到了前台,我想检测whtherXYZ已经启动与否。说有人推出的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. :/

在此先感谢, 萨米特。

Thanks in advance, Sumit.

推荐答案

不知道这是做最好的方法,但它的工作原理,你可以使用的logcat 并期待在其输出端,您可以使用此权限

not sure if this is the best way to do this but it works, you can use the logcat and look at its output, you can use this permission

android.permission.READ_LOGS

和使用低于code:

try
{
    Process mLogcatProc = null;
    BufferedReader reader = null;
    mLogcatProc = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});

    reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));

    String line;
    final StringBuilder log = new StringBuilder();
    String separator = System.getProperty("line.separator"); 

    while ((line = reader.readLine()) != null)
    {
        log.append(line);
        log.append(separator);
    }
    String w = log.toString();
    Toast.makeText(getApplicationContext(),w, Toast.LENGTH_LONG).show();
}
catch (Exception e) 
{
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}

不要忘记其权限的Andr​​oid清单,你可以添加这个code到一个线程为它工作。

and do not forget its permission on android manifest, you can add this code to a thread for it to work

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

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