How to Get the Sender of an Intent

How to get the sender of an Intent?

There may be another way, but the only solution I know of is having Activity A invoke Activity B via startActivityForResult(). Then Activity B can use getCallingActivity() to retrieve Activity A's identity.

Android, find the sender of an Intent for a monitoring application


I'm developing a monitoring application for Android listening for broadcast intents

I sincerely hope that you fail in your quest, as this should not be possible, for obvious privacy and security reasons.

I'm asking if there is a workaround to this, for instance looking into the context or whatsoever.

AFAIK, no, short of creating your own custom firmware.

How to get the sending intent activity's name?

Put the sender activity's name as an extra too.
For example:

intent.putExtra("ACTIVITY_NAME_BUNDLE_ID", "FancyActivityName");

And in the receiver activity's onCreate method handle this extra:

String senderActivityName = getIntent().getStringExtra("ACTIVITY_NAME_BUNDLE_ID");

How to determine the sender of Broadcast Intent

No.

If you only want to do something when an action was broadcast by another app (i.e. not yours), I imagine it should be easy to determine that your app didn't send this broadcast, and therefore it was someone else..

How to find Intent source in Android?

A better way to do this would be to use Intent extras to pass parameters to the receiver.

Find URL of sender of intent in a Browsable activity


I want to get the URL which user came from it to my app from browser

My interpretation of this is:

  • You have a Web page (e.g., https://hamidreza.com/somepage)
  • On that Web page, you have a link with a custom scheme (e.g., hamidreza://something)
  • On some devices, you have an app with an <activity> with an <intent-filter> that advertises support for that custom scheme
  • When the user visits that Web page from a browser on their device, and they click that link, which starts your activity... you want to be able to find out that this all started with https://hamidreza.com/somepage

You are welcome to examine the extras on the Intent that is delivered to your activity (via getIntent()) and see if the referer URL is in there somewhere. Perhaps for a few browsers, it is. I expect that for most browsers, it is not. And for those, there is no way for you to know the URL that contained the URL that started your activity.

As Selvin pointed out in a comment, you are welcome to embed that sort of information in your custom URL (e.g., hamidreza://something?referer=https://hamidreza.com/somepage). Then you can obtain that from query parameters on the Uri delivered to your activity.

Get IntentSender object for createChooser method in Android

The chooser target intent is not a PendingIntent. For instance, in the following snippet, I am declaring intent for ACTION_SEND, with type text/plain, and this is the my target intent for the Intent.createChooser. Then I am creating another Intent, receiver, and a handler, the PendingIntent, which will invoke onReceive of my BroadcastReceiver after the user picks something from the chooser.

// Set up the broadcast receiver (preferably as a class member)
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Unregister self right away
context.unregisterReceiver(this);

// Component will hold the package info of the app the user chose
ComponentName component = intent.getParcelableExtra<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT);
// Do something with component
}
}


Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");

// Use custom action only for your app to receive the broadcast
final String shareAction = "com.yourdomain.share.SHARE_ACTION";
Intent receiver = new Intent(shareAction);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());

// Before firing chooser activity, register the receiver with our custom action from above so that we receive the chosen app
context.registerReceiver(broadcastReceiver, new IntentFilter(SHARE_ACTION));

startActivity(chooser);

Edit:

The information, in the case of the BroadcastReceiver is embedded in the intent you get as parameter. After you selected one of the option, retrieve the Bundle's extras and using the key Intent.EXTRA_CHOSEN_COMPONENT, you should be able to find what the user picked.

Try adding as simple Log.d to onReceive

for (String key : intent.getExtras().keySet()) {
Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
}

In my example I got

ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}

for Telegram and

ComponentInfo{com.google.android.apps.inbox/com.google.android.apps.bigtop.activities.ComposeMessageActivity}

for InBox

What is the purpose of IntentSender?

IntentSender is kind of a level of abstraction or glue class that allows you to

  1. Receive broadcast when user selects application in chooser.

    Example when you use IntentSender:

    Intent intent = new Intent(Intent.ACTION_SEND)
    .putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
    .setType("text/plain");
    Intent receiver = new Intent(this, BroadcastTest.class)
    .putExtra("test", "test");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
    Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());
    startActivity(chooser);
  2. Start Activity with IntentSender instead of Intent (more in Android docs)

    startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)

    Like startActivity(Intent, Bundle), but taking a IntentSender to start.



Related Topics



Leave a reply



Submit