在Android中没有任何操作栏的ShareActionProvider

use*_*444 8 android android-intent android-layout android-actionbar

我不想在我的应用程序中使用操作栏,但仍希望拥有操作栏提供的共享按钮.

当操作栏在那里时完成.

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    ShareActionProvider provider = (ShareActionProvider)
    menu.findItem(R.id.menu_share).getActionProvider();

    if (provider != null) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "hi");
        shareIntent.setType("text/plain");
        provider.setShareIntent(shareIntent);
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

menu.xml保存在菜单文件夹中.

我想在我的xml中使用我自己的共享按钮,其中还定义了其他布局.

任何帮助?

Jus*_*ris 8

您不需要操作栏来共享内容.实际上,即使使用Action Bar,大多数应用程序也不会使用ShareActionProvider它,因为视觉设计师讨厌它并且它不支持用户设备上的许多最新共享功能(例如直接共享给联系人).相反,您应该使用Intent.createChooser创建更强大的共享对话框.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Run Code Online (Sandbox Code Playgroud)

http://developer.android.com/training/sharing/send.html

从应用程序的任何位置分享更好的方法是使用ShareCompat.这是一个简单的例子:

ShareCompat.IntentBuilder.from(this)
           .setType("text/plain")
           .setText("I'm sharing!")
           .startChooser();
Run Code Online (Sandbox Code Playgroud)

其他示例可以在这里找到:https://android.googlesource.com/platform/development/+/master/samples/Support4Demos/src/com/example/android/supportv4/app/SharingSupport.java


Com*_*are 5

使用PackageManagerqueryIntentActivities()查找知道如何处理ACTION_SEND Intent要调用的应用程序.根据需要显示结果列表.当用户进行选择时,创建一个等效项ACTION_SEND Intent,您可以在其中指定ComponentName用户选择的特定活动,并进行调用startActivity().