【问题标题】:What is the purpose of IntentSender?IntentSender 的目的是什么?
【发布时间】:2012-03-06 11:55:34
【问题描述】:

我想知道IntentSender 类对我们的应用程序的用途是什么?我们如何在我们的应用程序中使用它?

除了The Android Intent Based APIs: Part Seven – IntentSenders And PendingIntents,还有什么好的例子吗?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    IntentSender 是一种抽象级别或粘合类,允许您

    1. 当用户在选择器中选择应用程序时接收广播。

      使用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. IntentSender 开始Activity 而不是Intent(更多内容请参见Android docs

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

      类似于startActivity(Intent, Bundle),但以IntentSender 开头。

    【讨论】:

    • 感谢您的回答。很高兴注意到开发人员可以在其接收器onReceive() 方法中使用(ComponentName) intent.getExtras().getParcelable(EXTRA_CHOSEN_COMPONENT) 来获取所选的应用程序信息(包名称等...)。
    【解决方案2】:

    IntentSender 的官方 Android 开发者文档明确指出:

    不能直接创建此类的实例,而必须从现有的PendingIntentPendingIntent.getIntentSender() 创建。

    因此,您将(不应该)看到此类在代码示例或教程中直接使用。

    至于PendingIntent,它基本上是您提供给另一个应用程序的令牌,允许该应用程序使用您的应用程序的权限来执行您应用程序的特定代码。

    Here's an examplePendingIntent 在类中使用。

    【讨论】:

      猜你喜欢
      • 2011-03-05
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 2018-11-04
      • 2015-01-17
      • 2021-11-05
      相关资源
      最近更新 更多