0

我正在创建一个 android 应用程序,其中我已经准备好添加一个共享选项来共享应用程序的内容,但我想添加另一个共享选项,它将能够共享应用程序下载链接(共享此应用程序),这两个选项都使用在创建选项菜单上,谁能告诉我是否可以在创建选项上添加两个,或者是否有其他方法可以添加第二个共享操作。以下是我用于“共享此应用程序”操作的代码。

 private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);

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

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

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

主菜单

<item
    android:title="Share"
    android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:icon="@drawable/share"
    />

<item
    android:id="@+id/share_this_app"
    android:title="share this app"
    android:showAsAction="never"
    android:actionProviderClass="android.widget.ShareActionProvider"/>
4

1 回答 1

0

你有没有尝试过这样的事情?让我知道它是否有效。

private ShareActionProvider mShareActionProvider;
private ShareActionProvider mShareActionProvider2;

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

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

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

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

private Intent getDefaultShareIntent2(){
    /*Your coude here*/
}
于 2016-06-28T13:37:07.743 回答