What is the purpose of IntentSender?

依然范特西╮ 提交于 2020-01-10 08:09: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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9583230/what-is-the-purpose-of-intentsender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!