0

由于一些对话框问题,我不能使用 createChooser 方法来意图ACTION_SEND,我必须在具有共享操作的标准 Activity 上构建我自己的列表视图。

但是,我发现只有菜单项具有 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;
}
4

1 回答 1

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);
    }
}
于 2013-09-12T10:54:24.660 回答