public class ShareActionProvider
extends ActionProvider
| java.lang.Object | ||
| android.view.ActionProvider | ||
| android.widget.ShareActionProvider | ||
这是分享行动的提供者。 它负责创建视图以启用数据共享,并且如果托管项目放置在溢出菜单上,还可以显示带有共享活动的子菜单。
以下是如何在 MenuItem使用自定义支持文件的操作提供程序:
// In Activity#onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
// Get the menu item.
MenuItem menuItem = menu.findItem(R.id.my_menu_item);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
// Set history different from the default before getting the action
// view since a call to MenuItem.getActionView() calls
// onCreateActionView() which uses the backing file name. Omit this
// line if using the default share history file is desired.
mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");
. . .
}
// Somewhere in the application.
public void doShare(Intent shareIntent) {
// When you want to share set the share intent.
mShareActionProvider.setShareIntent(shareIntent);
}
注意:虽然示例代码片段演示了如何在菜单项的上下文中使用此提供程序,但提供程序的使用不限于菜单项。
也可以看看:
Nested classes |
|
|---|---|
interface |
ShareActionProvider.OnShareTargetSelectedListener 听众选择股票目标的事件。 |
常量(Constants) |
|
|---|---|
String |
DEFAULT_SHARE_HISTORY_FILE_NAME 存储共享历史记录的默认名称。 |
Public constructors |
|
|---|---|
ShareActionProvider(Context context) 创建一个新的实例。 |
|
公共方法(Public methods) |
|
|---|---|
boolean |
hasSubMenu() 确定此ActionProvider是否具有与其关联的子菜单。 |
View |
onCreateActionView() 此方法已弃用。 使用 |
void |
onPrepareSubMenu(SubMenu subMenu) 调用以为由此ActionProvider支持的菜单项准备相关的子菜单。 |
void |
setOnShareTargetSelectedListener(ShareActionProvider.OnShareTargetSelectedListener listener) 设置一个侦听器,以便在共享目标被选中时得到通知。 |
void |
setShareHistoryFileName(String shareHistoryFile) 设置文件的文件名以保存历史记录将用于排序共享目标的共享历史记录。 |
void |
setShareIntent(Intent shareIntent) 使用有关共享操作的信息设置意图。 |
继承方法(Inherited methods) |
|
|---|---|
android.view.ActionProvider
|
|
java.lang.Object
|
|
String DEFAULT_SHARE_HISTORY_FILE_NAME
存储共享历史记录的默认名称。
常量值:“share_history.xml”
ShareActionProvider (Context context)
创建一个新的实例。
| 参数(Parameters) | |
|---|---|
context |
Context: Context for accessing resources. |
boolean hasSubMenu ()
确定此ActionProvider是否具有与其关联的子菜单。
关联的子菜单将在操作视图不显示时显示。 在向onPrepareSubMenu(SubMenu)调用onPerformDefaultAction()以及向用户显示子菜单之前,此提供者实例将接收到对onPrepareSubMenu(SubMenu)的调用。
| 返回(Returns) | |
|---|---|
boolean |
true if the item backed by this provider should have an associated submenu |
View onCreateActionView ()
此方法已弃用。
使用onCreateActionView(MenuItem)
由Android框架调用的工厂方法来创建新的操作视图。
此方法已被弃用,以支持onCreateActionView(MenuItem) 。 希望支持API 16之前的平台版本的新应用程序也应实现此方法以返回有效的操作视图。
| 返回(Returns) | |
|---|---|
View |
A new action view. |
void onPrepareSubMenu (SubMenu subMenu)
调用以为由此ActionProvider支持的菜单项准备相关的子菜单。
如果hasSubMenu()返回true,则在选择菜单项准备用于呈现给用户的子菜单时将调用此方法。 应用程序可以使用它在显示之前创建或更改子菜单内容。
| 参数(Parameters) | |
|---|---|
subMenu |
SubMenu: Submenu that will be displayed |
void setOnShareTargetSelectedListener (ShareActionProvider.OnShareTargetSelectedListener listener)
设置一个侦听器,以便在共享目标被选中时得到通知。 侦听器可以选择决定处理选择,而不依赖于启动活动的默认行为。
注意:如果您选择后台共享历史记录文件,您仍将在此回调中收到通知。
| 参数(Parameters) | |
|---|---|
listener |
ShareActionProvider.OnShareTargetSelectedListener: The listener. |
void setShareHistoryFileName (String shareHistoryFile)
设置文件的文件名以保存历史记录将用于排序共享目标的共享历史记录。 该文件将用于由onCreateActionView()创建的所有视图。 默认为DEFAULT_SHARE_HISTORY_FILE_NAME 。 如果共享历史记录不应在会话之间持续存在,则设置为null 。
注意:历史文件名称可以随时设置,但只有在设置文件名后由onCreateActionView()创建的操作视图才会由提供的文件提供支持。 因此,如果您想要使用不同的历史记录文件来共享特定类型的内容,则每次更改历史记录文件setShareHistoryFileName(String)都必须调用invalidateOptionsMenu()来重新创建操作视图。 你不应该叫invalidateOptionsMenu()从onCreateOptionsMenu(Menu) 。
private void doShare(Intent intent) {
if (IMAGE.equals(intent.getMimeType())) {
mShareActionProvider.setHistoryFileName(SHARE_IMAGE_HISTORY_FILE_NAME);
} else if (TEXT.equals(intent.getMimeType())) {
mShareActionProvider.setHistoryFileName(SHARE_TEXT_HISTORY_FILE_NAME);
}
mShareActionProvider.setIntent(intent);
invalidateOptionsMenu();
}
| 参数(Parameters) | |
|---|---|
shareHistoryFile |
String: The share history file name. |
void setShareIntent (Intent shareIntent)
使用有关共享操作的信息设置意图。 以下是构建共享意向的示例:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri));
| 参数(Parameters) | |
|---|---|
shareIntent |
Intent: The share intent. |
也可以看看: