android 中所有应用的共享链接按钮

Share links button for all apps in android

我的 android 项目中有下一个共享 ImageView 按钮:

final ImageView share = findViewById(R.id.btn_share);
 share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent myIntent = new Intent(Intent.ACTION_SEND);
                myIntent.setType("text/plane");
                myIntent.putExtra(Intent.EXTRA_TEXT, some_url_from_my_app);
                startActivity(Intent.createChooser(myIntent, "Share Using"));
            }
        });

我的问题是,当我点击按钮时,我只看到几个应用程序,而不是我想要的 phone 中的所有应用程序,例如 Facebook、Instagarm 等...

我应该怎么做才能看到我 phone 中的所有应用程序以便能够与所有人共享 link?

把startActivity这一行拿出来。

在 res 文件夹中创建一个名为 menu 的 XML 资源文件夹。 创建布局 menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/share_menu">
    <item
        android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass=
            "android.widget.ShareActionProvider" />
</menu>

确保覆盖 onCreateOptionsMenu:

import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
...
private ShareActionProvider mShareActionProvider;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file.
    // R.<XML resource folder>.<XML layout filename>
    getMenuInflater().inflate(R.menu.menu, menu);

    

    // Locate MenuItem with ShareActionProvider
    MenuItem shareItem = menu.findItem(R.id.menu_item_share);

    // Fetch and store ShareActionProvider
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

    // Return true to display menu
    return true;
}

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

经过一些调查和几个小时后,我设法解决了这个问题。 我所做的是而不是做:

myIntent.setType("text/plane");

我做了这样的:

myIntent.setType("text/*");

这向我展示了所有用于共享的应用程序。 还有关于Facebook分享我用的是Facebook sdk