How to get the sender of an Intent?

Viewed 45888

Is there a way for an Activity to find out who (i.e. class name) has sent an Intent? I'm looking for a generic way for my Activity to respond to a received intent by sending one back to the sender, whoever that may be.

8 Answers

In my case, neither the accepted here and another most voted answer works perfectly.

Activity.getCallerActivity() works only for the sender which starts your activity by startActivityForResult, meaning that if the sender is also in your app and you have full control, it works, but not every external app starts others in that way.

Another most voted answer provides the solution for external app, but it too has issue. First I would prefer getAuthority() instead of getHost(), secondly, if the sender is a browser kind of app, like Chrome, both host and authority will give you the browsing web page's address host, such as www.google.com, instead of the app itself. So it depends on how you define 'sender', if you need to find out which web page starts you, the authority/host is good enough, but if you need to find out which app starts you, I am afraid authority/host can be trusted only when getScheme() gives you android-app instead of http.

Use UsageStatsManager and the old RecentTaskInfo to get the intent sender for OnCreate or onNewIntent:

    public static String getTopMostThirdPartyPackage(Context context) {
        String thisPak = null, tmp, top = null;
        try {
            thisPak = context.getPackageName();
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                UsageStatsManager man = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
                long now = System.currentTimeMillis();
                UsageEvents uEvts = man.queryEvents(now - 5000,now); // query in 5 sec
                UsageEvents.Event e = new UsageEvents.Event();
                while (uEvts.getNextEvent(e)){
                    tmp = e.getPackageName();
                    if (!thisPak.equals(tmp)) {
                        top = tmp;
                        break;
                    }
                }
            } else {
                ActivityManager man = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RecentTaskInfo> tasks = man.getRecentTasks(3, 0);
                for(ActivityManager.RecentTaskInfo info:tasks) {
                    tmp = info.baseIntent.getComponent().getPackageName();
                    if (!thisPak.equals(tmp)) {
                        top = tmp;
                        break;
                    }
                }
            }
        } catch (Exception e) {
        }
        return top;
    }

permissions :

    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
    <uses-permission android:name="android.permission.GET_TASKS" />
        intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        startActivity(intent);
Related