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