【发布时间】:2012-03-06 11:55:34
【问题描述】:
我想知道IntentSender 类对我们的应用程序的用途是什么?我们如何在我们的应用程序中使用它?
除了The Android Intent Based APIs: Part Seven – IntentSenders And PendingIntents,还有什么好的例子吗?
【问题讨论】:
我想知道IntentSender 类对我们的应用程序的用途是什么?我们如何在我们的应用程序中使用它?
除了The Android Intent Based APIs: Part Seven – IntentSenders And PendingIntents,还有什么好的例子吗?
【问题讨论】:
IntentSender 是一种抽象级别或粘合类,允许您
当用户在选择器中选择应用程序时接收广播。
使用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);
以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) 来获取所选的应用程序信息(包名称等...)。
IntentSender 的官方 Android 开发者文档明确指出:
不能直接创建此类的实例,而必须从现有的
PendingIntent和PendingIntent.getIntentSender()创建。
因此,您将(不应该)看到此类在代码示例或教程中直接使用。
至于PendingIntent,它基本上是您提供给另一个应用程序的令牌,允许该应用程序使用您的应用程序的权限来执行您应用程序的特定代码。
Here's an example 的 PendingIntent 在类中使用。
【讨论】: