2

My app com.test.sample is invoked by an external app com.testExternal.outsideApp. The external app uses an intent to invoke the MainActivity of com.test.sample by calling the startActivityForResult. What I am looking for is a way to programmatically obtain the package name of the external app. I tried several ways like

String parentPackageName = this.getParent().getPackageName();
//This fails since the parent is always returning NULL.

String packageName = this.getIntent().getPackage();
//This returns the package name of the current application which is not what I want.

Is there any other way to get the package name of the caller app?

paddy
  • 47
  • 7
  • Let the calling app put its package name in the used intent. – greenapps Apr 14 '15 at 19:45
  • I understand. But the external app doesn't contain the package name in the incoming bundle, and by design, the external team who designs this app doesn't want to send it. The only information I get is the app name. – paddy Apr 14 '15 at 19:48
  • You did not say that before. Why not? How do you know the apps name? – greenapps Apr 14 '15 at 19:50
  • Have you tried this? http://stackoverflow.com/questions/15786926/get-package-name-for-the-application-share-intent – droidpl Apr 14 '15 at 19:53

1 Answers1

3

You can do it if your application was started with Activity#startActivityForResult by using Activity#getCallingActivity().

droidpl
  • 5,872
  • 4
  • 35
  • 47
  • It doesn't work if I want to know the app which has launched my app through share, as it has been mentioned in docs that -if the calling activity is not expecting a result (that is it did not use the startActivityForResult(Intent, int) form that includes a request code), then the calling package will be null. – coderzzz18 Jun 07 '19 at 07:15
  • If you check the answer, its already specified that the activity needs to be called by the caller using startActivityForResult for this method to work – droidpl Jun 08 '19 at 02:19