5

I can get the app ID from my running activity via activity.getTaskId(). It will report back 185. If I go to another app, and start my activity from a share button, it will be placed IN THAT apps stack. If I do activity.getTaskId() it will report back 192 for example. I am assuming that an application process can only have one main task stack associated with it. How do I get that tasks ID? I want to be able to know "Hey I'm running outside of your apps task stack".

I contemplated doing this by polling the taskId the first time my activity is created and set that as a member variable to my Application Class, but if my app is killed, and then started first from another application, it will have the incorrect task id as the "AppTaskStackId". I haven't found any API for this.

3 Answers3

2

A different approach might be to have both an exported and non-exported activity. The exported activity would simply be forwarded on to the non-exported activity, but with an extra denoting that it was started externally. And then when starting the activity internally, you always call the non-exported activity without that "isExternal" extra.

And then, in the non-exported activity, you can check for the existence of that extra to determine if the activity was started internally or externally.

JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • The only problem I have with this is that the launcher activity is exported, and so I go back to square one, because anyone can start that activity explicitly. This does make sense in the case where sharing is involved because then I would only add an intent filter to a new exported activity. I still feel like there should be a way to figure out the task id. Getting the base activity would also be helpful. –  Jul 18 '14 at 20:02
  • Is it possible to just add an extra to every intent of mine that will act like a "key"? Is there a unique application object that I could serialize and deserialize? –  Jul 18 '14 at 21:30
1

The only way I could find to get even close to what you are trying to accomplish would be with the following code in, say, your Activity's onCreate:

ActivityManager m = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(1);
if(!runningTaskInfoList.isEmpty()) {
    String callingPackageName = runningTaskInfoList.get(0).baseActivity.getPackageName();
}

Here callingPackageName would be set to the package name of your app if the activity has been invoked from another activity in your own app, or is the main activity of your app.

However, if the activity was started by another app, say using the share function, then callingPackageName would be the package name of the calling app, so all you have to do is check if this is equal to your app's package name by calling getPackageName().

Also note that:

  • Your app will now need the android.permission.GET_TASKS permission
  • The documentation for this method states:

Note: this method is only intended for debugging and presenting task management user interfaces. This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future.

So I'm not sure how reliable this is or if it is even useful to you.

Jeshurun
  • 22,940
  • 6
  • 79
  • 92
0

A recently (API 29) added TaskInfo:

https://developer.android.com/reference/android/app/TaskInfo

should help here.

Leszek
  • 1,181
  • 1
  • 10
  • 21