CCHAHI
CCHAHI

Reputation: 11

get the package name of the application that is calling my app

I am developing an application in android that create folders and stored files and images in these folders. Each folder have a list that contains the name of applications that are permitted to access these files and image. my question is what is the code that can give me the package name of the application that is accessing my app in order to compare it with the names of apps in the list of each folder. I succeed in getting the package name when the case is that other application retrieve data from my application (startActivityForResult). but i cant get the package name when the case is just view (startActivity)!!! I need to know the package name in both case. any help?

Upvotes: 1

Views: 1632

Answers (1)

David Wasser
David Wasser

Reputation: 95636

Sorry. That isn't possible.

When an Activity is started with startActivityForResult(), Android needs to know who to return the result to. It keeps this information in an internal data structure and makes the information available to the called Activity via getCallingActivity() and getCallingPackage().

However, when an Activity is started with startActivity(), Android doesn't store the information about the caller Activity because it doesn't need this information (because Android knows that it isn't going to return a result to the caller Activity) and therefore it isn't available to your Activity.

Note that activities can also be started by Service and BroadcastReceiver component, so there isn't always a "caller Activity".

This is either a flaw in the design of Android or a "security feature", depending on how you want to use it.

Upvotes: 1

Related Questions