user2591941
user2591941

Reputation: 1

how to stop an application and launch Activity from a service?

I am trying to detect the launch of some application like setting. I creat a service i can get the name of all application, and then start an Activity. I would like to stop the application launched and run the activity in foreground not in backgroound. the code used in my sevice is given below. the new Activity is launched but still in background.

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

        }

Upvotes: 0

Views: 365

Answers (1)

Semyon Danilov
Semyon Danilov

Reputation: 1773

You can do something like:

Process.killProcess( Process.processPid() );

But you need to know process's pId.

Upvotes: 1

Related Questions