我已经可以检测,如果服务其他应用程序启动。我为拨号程序制作了它。同样可以被任何包名取代。
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(this,"Service Started", Toast.LENGTH_LONG).show();
final String str = "";
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int phonelaunched = 0,phoneclosed =0;
int phonelaunches = 1;
@Override
public void run() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess: runningAppProcessInfo) {
Log.d(appProcess.processName.toString(),"is running");
if (appProcess.processName.equals("com.android.dialer")) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND /*isForeground(getApplicationContext(),runningAppProcessInfo.get(i).processName)*/){
if (phonelaunched == 0){
phonelaunched = 1;
Log.d(str,"dude phone has been launched");
}
else if (phoneclosed == 1){
phonelaunches++;
phoneclosed = 0;
Log.d(String.valueOf(phonelaunches),"dude that was counter");
}
}
else {
phoneclosed = 1;
Log.d(str,"dude phone has been closed");
}
}
}
}
},2000,3000);
return START_STICKY;
}
这里我会经历所有正在运行的任务,并检查它是否是我们的预期应用程序。如果是这样,我检查应用程序是否为前景,并且应用程序永远不会使用'phonelaunched'变量启动。当预期的应用程序在后台并且相应地设置变量时使用phoneclosed。
这一切都是服务类
你为什么要这么做实现的?引用“**,以便我可以控制其上的流量**”这听起来可能。 – t0mm13b 2012-07-05 14:43:42
我正在尝试构建一个高中项目的应用程序。没有恶意软件。 :( – 2012-07-05 14:54:37
http://stackoverflow.com/questions/3290936/android-detect-when-other-apps-are-launched – SMR 2014-05-17 05:14:39