如何知道何时另一个应用开始启动? [英] How to know when another app is begin launching?

查看:111
本文介绍了如何知道何时另一个应用开始启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/4838546/androidhow-does-application-protector-app-works\">Android:how做应用程序保护工作?

如何能在新的应用程序开始启动通知我?明确地说,我点击应用程序图标,应用程序启动开始,我想我的服务设置为事件的观察者(如果有的话)。是否有任何事件或某种方式来了解该应用程序之前推出?

How can i be notified when new app begin launching? clearly to say, i click on app icon , and app begin launching, i want to set my service to observer of that event (if there is any). Is there any event or some way to know that before app launched ?

推荐答案

如果我是你,我会得到的 ActivityManager 通过调用

If I were you, I would get the ActivityManager with a call to

getSystemService(Context.ACTIVITY_SERVICE)

然后设置我的程序有以下code:

and then setup my program with the following code:

private final static Handler updateHandler = new Handler();
private static long WAKEUP_INTERVAL = 10000;    // 10secs?

private Runnable periodicUpdate = new Runnable() {
    @Override
    public void run() {
        checkRunningProcesses();
        updateHandler.postDelayed(this, WAKEUP_INTERVAL);
    }
};

做以下两件事情定期检查:

to do periodical checks of the following two things:


  1. 检查<一href=\"http://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks%28int,%20int%29\"相对=nofollow> getRecentTasks()找出用户最近推出(requres的 GET_TASKS 许可)

  2. 从<α
  3. 获得href=\"http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses%28%29\"相对=nofollow> getRunningAppProcesses()并保持以防万一定期检查间隔正在运行的进程列表中的某个地方已经选择了太长时间并增加了一些流程/删除,但在getRecentTasks没有显示()

  1. check getRecentTasks() to find out what user has launched recently (requres GET_TASKS permission)
  2. get from getRunningAppProcesses() and keep somewhere list of running processes just in case periodical check interval has been chosen too long and some processes were added/removed but did not show in getRecentTasks()

此方式,我无法获得有关程序启动即时通知,但能找到的关于很快(秒后,也许)做什么。

This way I could not get an instant notification about program launch, but could find about that soon enough (seconds later, maybe) to do something about it.

还有一件事,它可能是一个明智的想法时,屏幕变暗,以节省电池停车检查。

One more thing, it might be a wise idea to stop checks when screen goes dark, to save the battery.

这篇关于如何知道何时另一个应用开始启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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