gpt4 book ai didi

android - 应用程序启动时的监听器?

转载 作者:行者123 更新时间:2023-11-29 21:02:47 25 4
gpt4 key购买 nike

有没有什么方法可以实现一个在任何应用程序启动时收到通知的监听器?不是我的应用程序任何应用程序启动或停止?

如果不可能,是否有办法查明特定应用程序是否已启动或停止,即当 facebook 应用程序(例如)启动或停止时的监听器?

最佳答案

使用跟踪所有正在运行的应用程序的 STICKY 服务

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {

public void run()
{
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < services.size(); i++) {
if(!stalkList.contains(services.get(i).baseActivity.getPackageName()))
{
// you may broad cast a new application launch here.
stalkList.add(services.get(i).baseActivity.getPackageName());
}
}

List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++) {

ArrayList<String> runningPkgs = new ArrayList<String>(Arrays.asList(procInfos.get(i).pkgList));

Collection diff = subtractSets(runningPkgs, stalkList);

if(diff != null)
{
stalkList.removeAll(diff);
}
}


}
}, 20000, 6000); // every 6 seconds


return START_STICKY;
}




private RunningAppProcessInfo getForegroundApp() {
RunningAppProcessInfo result = null, info = null;

final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

List <RunningAppProcessInfo> l = activityManager.getRunningAppProcesses();
Iterator <RunningAppProcessInfo> i = l.iterator();
while(i.hasNext()) {
info = i.next();
if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& !isRunningService(info.processName)) {
result = info;
break;
}
}
return result;
}

private boolean isRunningService(String processName) {
if(processName == null)
return false;

RunningServiceInfo service;

final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

List <RunningServiceInfo> l = activityManager.getRunningServices(9999);
Iterator <RunningServiceInfo> i = l.iterator();
while(i.hasNext()){
service = i.next();
if(service.process.equals(processName))
return true;
}
return false;
}

private boolean isRunningApp(String processName) {
if(processName == null)
return false;

RunningAppProcessInfo app;

final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

List <RunningAppProcessInfo> l = activityManager.getRunningAppProcesses();
Iterator <RunningAppProcessInfo> i = l.iterator();
while(i.hasNext()){
app = i.next();
if(app.processName.equals(processName) && app.importance != RunningAppProcessInfo.IMPORTANCE_SERVICE)
return true;
}
return false;
}


private boolean checkifThisIsActive(RunningAppProcessInfo target){
boolean result = false;
ActivityManager.RunningTaskInfo info;

if(target == null)
return false;

final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

List <ActivityManager.RunningTaskInfo> l = activityManager.getRunningTasks(9999);
Iterator <ActivityManager.RunningTaskInfo> i = l.iterator();

while(i.hasNext()){
info=i.next();
if(info.baseActivity.getPackageName().equals(target.processName)) {
result = true;
break;
}
}

return result;
}


// what is in b that is not in a ?
public static Collection subtractSets(Collection a, Collection b)
{
Collection result = new ArrayList(b);
result.removeAll(a);
return result;
}

关于android - 应用程序启动时的监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25500562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com