我正在尝试检测某些应用程序的启动,例如设置。我创建了一个服务,我可以获取所有应用程序的名称,然后启动一个 Activity。我想停止启动的应用程序并在前台而不是在后台运行活动。我的服务中使用的代码如下所示。新活动已启动但仍在后台。
public int onStartCommand(Intent intent, int flags, int startid) {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
try {
ActivityManager am = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = am
.getRunningTasks(Integer.MAX_VALUE);
Log.d("liste des applications ", recentTasks.toString());
for (int i = 0; i < recentTasks.size(); i++) {
Log.d("Executed app",
"Application executed : "
+ recentTasks.get(i).baseActivity
.toShortString() + "\t\t ID: "
+ recentTasks.get(i).id + "");
Log.d("============Executed app ======", recentTasks
.get(i).topActivity.getShortClassName());
if ((recentTasks.get(i).topActivity.getShortClassName()
.equals((".Settings"))) && (printed == false)) {
Log.d("*******====testestesttest====***********", recentTasks
.get(i).topActivity.getShortClassName());
Intent intent = new Intent(LogAnalyserService.this, LauncherGridActivity.class);
// intent.setClass(this, LauncherGridActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
printed = true;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}