Java Android - 如何检测用户是否选择使用意图分享到 Facebook 或 twitter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23494624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How do I detect if user has chosen to share to Facebook or twitter using intent?
提问by
I've created an application which shares to facebook, twitter etc. But I want to perform different functionsdependent on who the user is sharing to, for instance if the user is sharing to Facebook do one thing but if the user shares to twitter do another.
我创建了一个共享到 facebook、twitter 等的应用程序。但是我想根据用户共享给谁来执行不同的功能,例如,如果用户共享到 Facebook 做一件事,但如果用户共享到 twitter 做其他。
How do I do this?
我该怎么做呢?
My code so far is below:
到目前为止,我的代码如下:
private void ShareSub() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(
Intent.EXTRA_TEXT,
"You've just shared "
+ "Awesome");
startActivity(i);
}
Further explanation of above code:
对以上代码的进一步解释:
When the user presses the "menu_item_share" button in the action bar, the function above is triggered. This function then makes a box like the image below appear, which allows the user to select a particular option, e.g. Facebook or twitter. The Intent.EXTRA_TEXT
is then shared to that particular app.
当用户按下操作栏中的“ menu_item_share”按钮时,将触发上述功能。该函数随后会显示一个如下图所示的框,允许用户选择特定选项,例如 Facebook 或 twitter。所述Intent.EXTRA_TEXT
然后共享于该特定应用程序。
What I'm wanting is, when the user clicks for instance Facebook, a particular function is called e.g. Sharing via their (Facebook's) api etc.
我想要的是,当用户点击例如 Facebook 时,会调用一个特定的功能,例如通过他们(Facebook 的)api 共享等。
I'm not using ShareActionProviderlike below:
我没有像下面那样使用 ShareActionProvider:
<item
android:id="@+id/your_share_item"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="@string/share"/>
Instead, I've created a simple button which calls the function ShareSub:
相反,我创建了一个调用函数ShareSub的简单按钮:
<item
android:id="@+id/menu_item_share"
android:icon="@android:drawable/ic_menu_share"
android:showAsAction="ifRoom"
android:title="Share"/>
The reason I've chosen to do it this way is, I don't want the recently shared tobutton to appear, as in the image below. This is causing me problems when I attempt to use the code suggested below because I haven't used ShareActionProvider.
我选择这样做的原因是,我不希望最近共享到按钮出现,如下图所示。当我尝试使用下面建议的代码时,这给我带来了问题,因为我没有使用ShareActionProvider。
采纳答案by Nachi
You can't directly find out which app was chosen, but you can display your own Chooser dialog.
您无法直接找出选择了哪个应用程序,但您可以显示自己的选择器对话框。
Instead of calling startActivity(i)
, get a list of all activities (facebook, twitter etc) that are registered to handle your intent using queryIntentActivities().
startActivity(i)
使用queryIntentActivities() 获取注册以处理您的意图的所有活动(facebook、twitter 等)的列表,而不是调用。
List<ResolveInfo> activities = getPackageManager().queryIntentActivities (i, 0);
Each ResolveInfo
is an app that can handle your intent. Now you can show an AlertDialogcontaining a list of labels or icons. When the user selects a label from the list, you can handle what do to on an app-specific basis in the click handler for the dialog.
每个ResolveInfo
都是可以处理您的意图的应用程序。现在您可以显示一个包含标签或图标列表的AlertDialog。当用户从列表中选择一个标签时,您可以在对话框的单击处理程序中处理特定于应用程序的操作。
EDIT: Here's a full working example
编辑:这是一个完整的工作示例
private void ShareSub() {
final Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,"text");
final List<ResolveInfo> activities = getPackageManager().queryIntentActivities (i, 0);
List<String> appNames = new ArrayList<String>();
for (ResolveInfo info : activities) {
appNames.add(info.loadLabel(getPackageManager()).toString());
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Complete Action using...");
builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = activities.get(item);
if (info.activityInfo.packageName.equals("com.facebook.katana")) {
// Facebook was chosen
} else if (info.activityInfo.packageName.equals("com.twitter.android")) {
// Twitter was chosen
}
// start the selected activity
i.setPackage(info.activityInfo.packageName);
startActivity(i);
}
});
AlertDialog alert = builder.create();
alert.show();
}
回答by Engr Waseem Arain
Try that answers: here choose the define intent which you want:
试试这个答案:在这里选择你想要的定义意图:
Customize Android Intent.ACTION_SEND
自定义 Android Intent.ACTION_SEND
Branching the Android Share Intent extras depending on which method they choose to share
根据他们选择共享的方法来分支 Android Share Intent extras
Android - How to filter specific apps for ACTION_SEND intent
回答by adneal
Use presses a button in the action bar which triggers the above function.
使用按下触发上述功能的操作栏中的按钮。
If you used a ShareActionProvider
instead, you could detect when a user selects a particular app from the list by implementing a ShareActionProvider.OnShareTargetSelectedListener
. Otherwise you won't be able to detect which app is selected unless you implement your own activity chooser.
如果您改用 a ShareActionProvider
,则可以通过实现ShareActionProvider.OnShareTargetSelectedListener
. 否则,除非您实现自己的活动选择器,否则您将无法检测到选择了哪个应用程序。
Here's an example:
下面是一个例子:
menu
菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/your_share_item"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="@string/share"/>
</menu>
Implementation
执行
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your menu
getMenuInflater().inflate(R.menu.share, menu);
// The Intent you want to share
final Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "Test");
// Set up your ShareActionProvider
final ShareActionProvider shareActionProvider = (ShareActionProvider) menu.findItem(
R.id.your_share_item).getActionProvider();
shareActionProvider.setShareIntent(i);
shareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
final String packageName = intent.getComponent().getPackageName();
if (packageName.equals("com.facebook.katana")) {
// Do something for Facebook
} else if (packageName.equals("com.twitter.android")) {
// Do something for Twitter
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}