4

I would like to get the application name or its package name that create the shared intent. I'm making application that receive shared data via intent, how could I get the package name of the creator application? something like [intent.getSourceBackageName()]

EDIT: Im not sure if my question is clear. But I need to find out the name of the app that called mine, for example, if I share from Browser, I need to find a way to know that Browser app is the application which share data with me.

Ghaleb Khaled
  • 180
  • 1
  • 10
  • possible duplicate of [Android get previous activity](http://stackoverflow.com/questions/8119526/android-get-previous-activity) – njzk2 Apr 03 '13 at 12:09
  • 1
    no its not. My case is different, since I receive the intent from other apps like Facebook app. So I cant put 'from' extra! – Ghaleb Khaled Apr 03 '13 at 13:05
  • 1
    actually, yes, it is. the question I pointed to contains several answers, including one explaining that getting the calling package is possible only if the call was made by use of startActivityForResult, by the use of getCallingActivity() or getCallingPackage(). – njzk2 Apr 03 '13 at 15:21

7 Answers7

4

how could I get the package name of the creator application?

You can't. For example, there may not be a "creator application", as the ACTION_SEND Intent could be sent from the command line (adb shell am). And, since ACTION_SEND is not used with startActivityForResult(), njzk2's solution will not work.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • There may indeed not be a 'creator application', but that doesn't mean an intent can never contain info about its creator. That info could be missing / not set / null / empty in that case. – Erik Feb 28 '19 at 11:51
2

Getting the package-name of the app that started your Activity:

/**
 * 
 * @param activity
 * @return The calling application's package name or {@code null} if the activity was not
 *         started with {@link #startActivityForResult}
 */
public static String getCallingPackageName(Activity activity) {
    return activity.getCallingPackage();
}

Getting the app name that started your Activity:

/**
 * Get the name of the app that started this Activity.
 * 
 * @param activity
 * @return The calling application's app name or {@code null} if the activity was not started
 *         with {@link #startActivityForResult}
 */
public static String getCallingAppName(Activity activity) {
    final String packageName = activity.getCallingPackage();
    if (packageName == null) {
        return null;
    }
    final PackageManager pm = activity.getPackageManager();
    try {
        return pm.getApplicationInfo(packageName, 0).loadLabel(pm).toString();
    } catch (NameNotFoundException ignored) {
    }
    return null;
}

Notes:

Tested by creating an activity with the following intent filter:

<intent-filter>
    <action android:name="android.intent.action.GET_CONTENT" />

    <data android:mimeType="*/*" />

    <category android:name="android.intent.category.OPENABLE" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Opened Gmail -> composed a new message and selected "Attach file" -> chose the test app.

getCallingAppName(this) returned Documents

getCallingPackageName(this) returned com.android.documentsui


In my case it returned Documents instead of Gmail because Gmail opens Documents by default. So, you should consider that it may be impossible to really know the original app and the result will always be null unless your Activity was called with startActivityForResult

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • I actually call it from my activity through intent-filter similar to yours, and start it with startACtivityForResult(), however both getCallingPackage and getCallingActivity are returning null.. – obey Jan 14 '21 at 12:15
1

Given that it is possible to create an Intent without including the information you are looking for, it is impossible to guarantee being able to extract it. However, if you examine all the fields that arrive with the intent (explore intent.getCategories) you may be able to spot some patterns you can use to work out some cases ...

Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
1

You can get calling package using your Activity, try:

getActivity().getCallingPackage()
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
0

Use PackageManager class

final PackageManager packagemanager = getApplicationContext().getPackageManager();
ApplicationInfo app;
try {
    app = packagemanager.getApplicationInfo( this.getPackageName(), 0);

} catch (final NameNotFoundException e) {
    app= null;
}

 String applicationName = (String) (app!= null ? packagemanager.getApplicationLabel(app) : "(unknown)");

It gives the application name as defined in <application> tag of its manifest.

Nirali
  • 13,571
  • 6
  • 40
  • 53
0

Starting API 22, this method is available. It may be null and as documented can also be spoofed but it seems like the best bet API 22+

Nonos
  • 2,450
  • 2
  • 23
  • 34
-3

It's very simple

getPackageManager().getPackageInfo(getPackageName(), 0).packageName (or) MyContext.getPackageName()

  • This gives me my application package. I need to get the source application that create the intent. example, facebook share with me some data, I would like to know that facebook is the application that shared the data – Ghaleb Khaled Apr 03 '13 at 13:11
  • please refer this http://stackoverflow.com/questions/6758841/how-to-know-perticular-package-application-exist-in-the-device – AndroidEnthusiastic Apr 03 '13 at 13:29
  • I get back to the question you mentioned, but how knowing the package is exist or not can help me? – Ghaleb Khaled Apr 03 '13 at 13:54