0

In my app i want to check whethere any other app launches or not from my app.So i am currently usimg Activitymanager to find the top activity like this

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
         + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();

But i want to run this code in background service.So that i can detect When other app launches through this.So is there any idea to do this.Plz help me.thanks

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
Ekanta Swain
  • 473
  • 2
  • 13
  • 28
  • 1
    possible duplicate of [Android: How can I get the current foreground activity (from a service)?](http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service) – sschuberth Jul 09 '14 at 12:41

4 Answers4

3

By using service you can achieve this..

public void checkActivity() {
    handler = new Handler();
    activityRunnable = new ActivityRunnable();
    handler.postDelayed(activityRunnable, 500);
}

private class ActivityRunnable implements Runnable {
    @Override
    public void run() {
        ActivityManager manager = (ActivityManager) getApplicationContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTasks = manager.getRunningTasks(1);
        if (runningTasks != null && runningTasks.size() > 0) {
            ComponentName topActivity = runningTasks.get(0).topActivity;
            // Here you can get the TopActivity for every 500ms
            if(!topActivity.getPackageName().equals(getPackageName())){
                // Other Application is opened
            }
            handler.postDelayed(this, 500);
        }
    }
}

Call the checkActivity() in onCreate of service and dont forgot to remove the handler.removeCallbacks(activityRunnable); callbacks in onDestroy

start the service in LauncherActivity onCreate and stop the service in LauncherActivity onDestroy

Note: Dont forgot to add the permission in your manifest

<uses-permission android:name="android.permission.GET_TASKS" />
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • Hey thanks for ur reply.If i will use this then in every 500ms it ll launch again and again.So is there any other possibilities so that i can check whethere user opened any other app .So that i can get a toast that user opened that specific app. – Ekanta Swain Dec 23 '13 at 09:47
  • Then start the service where the possibility the user can launch the third party app from your app...or @Martin said...you can follow that approach – Jagadesh Seeram Dec 23 '13 at 09:50
  • Seems to be deprecated, won't work in Lollipop https://code.google.com/p/android-developer-preview/issues/detail?id=1168 – Groco Dec 10 '14 at 23:06
-1

You should use Service for this. Then only you can achieve the background process..Please Go through about Services http://developer.android.com/guide/components/services.html and you can see this also http://www.vogella.com/articles/AndroidServices/article.html both URLs will help you

Arun Kumar
  • 100
  • 1
  • 2
  • 12
  • Yeah..He want to check that in Background process..so,that i posted that URLs..What wrong with this???? – Arun Kumar Dec 23 '13 at 09:33
  • I want to run this code in service class.It's fine.But in which method exactly.in oncreate or onstart or in onstartcommand .So that whenever i ll switch from one activity to another i ll get a toast. – Ekanta Swain Dec 23 '13 at 09:36
  • His code is incorrect and you added the links later. Still the important thing here is that he should not run that code in a service because it will not work (and he should not run that code at all because it's wrong). The correct way to let a service know if the app went background is explained in the link I passed. The service has nothing to do with the problem. – Martin Marconcini Dec 23 '13 at 09:37
  • Actually i want to get a toast message when the user ll open gtalk application.that's all i want.So if it is possible then any help will apreciate.thanks arun – Ekanta Swain Dec 23 '13 at 10:20
  • For Example This GTalk app have open from your app..Then only you will get the Toast right?? – Arun Kumar Dec 23 '13 at 10:33
-1

instead of service Use receiver to receive the launch event and there you can implement this code.

Amit
  • 391
  • 3
  • 15
-2

The approach you are taking is wrong and prone to have your app rejected from the Google Play Store (plus it may stop working).

Check this thread to see possible ways to detect wether your app went to the background or not.

Community
  • 1
  • 1
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • Hello martin thanks for replying.But my question is I want to run this code in service class.So in which method excatly i will run this code so that it will run in background long time. – Ekanta Swain Dec 23 '13 at 09:32
  • The problem is not where you run the code, but the code itself. The code you wrote (which you copied and pasted after googling) is incorrect. The method getRunningTasks is not meant for production code (read the ActivityManager documentation for more information where this is correctly explained). If you want to know at a Service level when your app is in the background simply call your service with a specific event saying the app went background/foreground and save that in a local variable in the service. Do not copy/paste code without understanding what it does. – Martin Marconcini Dec 23 '13 at 09:35
  • Ohk exactly when other app will launch i want to get a toast message from my app.is it possible – Ekanta Swain Dec 23 '13 at 09:38
  • What is possible is you knowing when your app's UI has been hidden and is no longer visible. Please spend more than 20 seconds and READ the thread I liked and the Services documentation liked by Arun. – Martin Marconcini Dec 23 '13 at 09:39