kiran
kiran

Reputation: 3264

How to know which activity is presently opened?

I am new to android.

I am developing one app.

Based on that when my application will start,a back ground service will start.

And it run background and notifi the user,when user starts new activity.

For example user opens the camera,contacts..through the background service i want to get every time which activity is opened.

If any one has the solution,please help me

Thanks in advance.

Upvotes: 0

Views: 141

Answers (2)

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

this code will return All packages that have been loaded into the process .

see the results after lauching related apps and make changes .

    private String[] AppForground() {

            ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> l = mActivityManager
                    .getRunningAppProcesses();
            Iterator<RunningAppProcessInfo> i = l.iterator();
            while (i.hasNext()) {
                RunningAppProcessInfo info = i.next();
                if (info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    return info.pkgList;
                }
            }

            return null;
}
}

Upvotes: 2

Lucifer
Lucifer

Reputation: 29632

you can achieve this scenario using following steps. This is my view for solution.

  • make setter()/getter() method for activity name.

  • whenever you change your activity set that activity's name using set method.

  • now using getter() method you can see which is currently running activity.

Upvotes: 1

Related Questions