ShareActionProvider仅用于菜单项?

时间:2013-09-12 10:21:59

标签: android user-interface

由于某些对话框问题,我无法使用createChooser方法来强制 ACTION_SEND ,我必须在带有共享操作的标准活动上构建自己的列表视图。

但是,我发现只有菜单项有actionProviderClass属性:android:actionProviderClass="android.widget.ShareActionProvider"

我如何建立自己的股票行动清单?

下面是ShareActionProvider如何工作的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.share_menu, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return super.onCreateOptionsMenu(menu);

}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}

   /** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
    intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
    return intent;
}

1 个答案:

答案 0 :(得分:1)

您可以使用PackageManager.queryIntentActivities(Intent, int)获取可以处理给定Intent的活动和服务列表。

final List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(shareIntent, 0);
for (Iterator<ResolveInfo> iter = resolveInfos.iterator(); iter.hasNext();) {
    ResolveInfo resolveInfo = iter.next();
    if (resolveInfo.activityInfo != null) {
        final Drawable icon = resolveInfo.loadIcon(PackageManager);
        final String title = resolveInfo.loadLabel(PackageManager);
    }
}