28

I want to known what is the purpose of the IntentSender class for our application? How do we use it in our application?

Are there any good examples, apart from The Android Intent Based APIs: Part Seven – IntentSenders And PendingIntents?

Sufian
  • 6,405
  • 16
  • 66
  • 120
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213

2 Answers2

23

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
pixel
  • 24,905
  • 36
  • 149
  • 251
  • 3
    Thanks for your answer. Nice to notice that the developer can use `(ComponentName) intent.getExtras().getParcelable(EXTRA_CHOSEN_COMPONENT)` in its receiver `onReceive()` method to get the chosen application info (package name, etc ...). – Mir-Ismaili Mar 03 '17 at 08:09
6

The official Android developer documentation for IntentSender clearly states:

Instances of this class can not be made directly, but rather must be created from an existing PendingIntent with PendingIntent.getIntentSender().

So, you would(should) not see this class being used directly in a code sample or tutorial.

As for a PendingIntent, it's basically a token that you give to another application which allows that application to use your app's permissions to execute a specific piece of your app's code.

Here's an example of a PendingIntent used in a class.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
SBerg413
  • 14,515
  • 6
  • 62
  • 88