检测在Android中打开的应用程序

时间:2015-03-22 00:04:34

标签: java android oop logcat

我尝试构建一项服务,当用户在Android中启动任何应用时,该服务会向用户显示祝酒。

我看到的每个地方,我都看到人们建议使用logcat跟踪应用程序的启动 - 所以我认为这是正确的方法。

此外,发布的方法如下:

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();
}

但是,这并没有真正解释如何检测特定包或应用程序的打开。

所以,我的问题是:

如何从我的服务以编程方式扫描android的活动日志,以检测启动了哪些软件包。

因此,应该将输出转储到只包含已打开的应用程序包名称的数组中!

0 个答案:

没有答案
相关问题