public static @Nullable String getCallingPackage(final Activity activity) {
		Intent original_intent = null;
		final Intent intent = activity.getIntent();
		if (intent.hasExtra(Intent.EXTRA_REFERRER) || intent.hasExtra(Intent.EXTRA_REFERRER_NAME)) {
			original_intent = new Intent(activity.getIntent());
			intent.removeExtra(Intent.EXTRA_REFERRER);
			intent.removeExtra(Intent.EXTRA_REFERRER_NAME);
		}
		try {
			final Uri referrer = activity.getReferrer();		// getReferrer() returns real calling package if no referrer extras
			if (referrer != null) return referrer.getAuthority();        // Referrer URI: android-app://<package name>
		} finally {
			if (original_intent != null) activity.setIntent(original_intent);
		}
		if (Hacks.IActivityManager_getLaunchedFromPackage != null) try {
			final Object am = Hacks.ActivityManagerNative_getDefault.invoke().statically();
			if (am != null) {
				final IBinder token = Hacks.Activity_getActivityToken.invoke().on(activity);
				return token != null ? Hacks.IActivityManager_getLaunchedFromPackage.invoke(token).on(am) : null;
			}
		} catch (final Exception e) {
			Log.e(TAG, "Error detecting caller", e);
		}
		return null;
	}
Activity with enhanced {@link #getCallingPackage()}, which detects caller even if not started by {@link #startActivityForResult(Intent, int)}.

Created by Oasis on 2019-2-27.
shared/src/main/java/com/oasisfeng/island/util/CallerAwareActivity.java