createChooser方法我想用它使用IntentSender
Intent.createChooser法的新版本IntentSender對象。獲得在Android的
文檔只是說我可以從PendingIntent
例如抓住它。在我看來,PendingIntent
似乎沒有任何其他用途。
有另一種方式來獲得IntentSender
或是否需要創建PendingIntent
?
createChooser方法我想用它使用IntentSender
Intent.createChooser法的新版本IntentSender對象。獲得在Android的
文檔只是說我可以從PendingIntent
例如抓住它。在我看來,PendingIntent
似乎沒有任何其他用途。
有另一種方式來獲得IntentSender
或是否需要創建PendingIntent
?
選配目標的意圖並非PendingIntent
。例如,在下面的代碼片段中,我聲明瞭ACTION_SEND
的意圖,類型爲text/plain
,這是我的Intent.createChooser
的目標意圖。然後我創建另一個Intent
,接收器和處理器,該PendingIntet
,將調用我的東西從選擇器BroadcastTest
一個挑後onReceive
。
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");
Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());
startActivity(chooser);
編輯:
的信息,在BroadcastReceiver
的情況下被嵌入在你作爲參數的意圖。選擇其中一個選項後,檢索套件的附加功能並使用密鑰android.intent.extra.CHOSEN_COMPONENT
,您應該能夠找到用戶選擇的內容。
嘗試增加簡單Log.d到onReceive
for (String key : intent.getExtras().keySet()) {
Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
}
在我的例子我
ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}
爲Telegram
和
ComponentInfo{com.google.android.apps.inbox/com.google.android.apps.bigtop.activities.ComposeMessageActivity}
InBox
另一種方式來做到這一點。
/**
* Receiver to record the chosen component when sharing an Intent.
*/
static class TargetChosenReceiver extends BroadcastReceiver {
private static final String EXTRA_RECEIVER_TOKEN = "receiver_token";
private static final Object LOCK = new Object();
private static String sTargetChosenReceiveAction;
private static TargetChosenReceiver sLastRegisteredReceiver;
static boolean isSupported() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
static void sendChooserIntent(Activity activity, Intent sharingIntent) {
synchronized (LOCK) {
if (sTargetChosenReceiveAction == null) {
sTargetChosenReceiveAction = activity.getPackageName() + "/"
+ TargetChosenReceiver.class.getName() + "_ACTION";
}
Context context = activity.getApplicationContext();
if (sLastRegisteredReceiver != null) {
context.unregisterReceiver(sLastRegisteredReceiver);
}
sLastRegisteredReceiver = new TargetChosenReceiver();
context.registerReceiver(
sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction));
}
Intent intent = new Intent(sTargetChosenReceiveAction);
intent.setPackage(activity.getPackageName());
intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
final PendingIntent callback = PendingIntent.getBroadcast(activity, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Intent chooserIntent = Intent.createChooser(sharingIntent,
activity.getString(R.string.share_link_chooser_title),
callback.getIntentSender());
activity.startActivity(chooserIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
synchronized (LOCK) {
if (sLastRegisteredReceiver != this) return;
context.getApplicationContext().unregisterReceiver(sLastRegisteredReceiver);
sLastRegisteredReceiver = null;
}
if (!intent.hasExtra(EXTRA_RECEIVER_TOKEN)
|| intent.getIntExtra(EXTRA_RECEIVER_TOKEN, 0) != this.hashCode()) {
return;
}
ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT);
if (target != null) {
setLastShareComponentName(context, target);
}
}
}
你必須通過'PendingIntent'創建它。構造函數是公開的,但是用'@ hide'註釋@ – Blackbelt
@Blackbelt,但是我應該通過'PendingIntent'作爲目標'Intent'嗎? – pixel