-1

I want to bring another app to front if it is running already, otherwise I don't want to do anything.

Normally I start the other app like following:

ComponentName compName = new ComponentName(packageName, activityName);
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setComponent(compName);
context.startActivity(i);

If I remove the Intent.FLAG_ACTIVITY_NEW_TASK the call will throw an exception that indicates, that it is not allowed to start the activity without this flag from a non activity context...

Can I somehow else do what I want?

prom85
  • 16,896
  • 17
  • 122
  • 242

1 Answers1

0

First check whether the application is in running state.

 ActivityManager am = (ActivityManager) mContext
            .getSystemService(Activity.ACTIVITY_SERVICE);
    String packageName = am.getRunningTasks(1).get(0).topActivity
            .getPackageName();

This will return the package name of activities that are running in background.If the desired package name is running then call intent orelse do nothing.

Arun Inbasekaran
  • 297
  • 1
  • 3
  • 14
  • This won't work on all android versions... On newer android versions ypu'll need to use the `AccessibilityService` and this is a special permission and the user will have to enable this permission manually for your app... Doing it with the `AccessibilityService` is possible though and I'm doing it that way but I would like to find a solution that works for people that do not want to give that special permission to my app – prom85 Feb 12 '16 at 10:01