如何使用Intent在Android中创建共享按钮?

时间:2019-06-13 02:09:10

标签: android android-intent

我的应用程序有一个共享按钮,可共享指向Play商店中应用程序的链接。我是intent方法的新手,并不断收到此错误:

enter image description here

还有这个错误

任何关于如何正确创建共享按钮的输入都非常感激!下面是ActivityMain中的代码,在其下方; XML到共享按钮。

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

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
        // Inflate menu resource file.
        getMenuInflater().inflate(R.menu.main, menu);

        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.nav_share);

        // Fetch and store ShareActionProvider
        shareActionProvider = (ShareActionProvider) item.getActionProvider();

        // Return true to display menu
        return true;
    }

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

共享按钮XML

  <item android:title="Communication">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="@string/menu_share" />
            android:actionProviderClass=
            "android.widget.ShareActionProvider" />
        </menu>
    </item>

EDIT Image of share via tab goal

2 个答案:

答案 0 :(得分:3)

在菜单项/按钮上,单击以调用以下方法。

private void shareAppLink(){
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_VIEW);
    shareIntent.setData(Uri.parse("Link to app store"));
    startActivity(shareIntent);
}

答案 1 :(得分:0)

这是示例代码:

     <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="@string/menu_share" />
    </menu>



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.main, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.nav_share);

     item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            if (item.getItemId() == R.id.nav_share){
                ShareApp();
            }

            return false;
        }
    });

    // Fetch and store ShareActionProvider
    shareActionProvider = (ShareActionProvider) item.getActionProvider();

    // Return true to display menu
    return true;
}


 private void ShareApp() {
        try {
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
            String shareMessage = "\nLet me recommend you application\n\n";
            shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "\n\n";
            shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
            startActivity(Intent.createChooser(shareIntent, "choose one"));
        } catch (Exception e) {
            DebugLog.e(e.getMessage());
            e.printStackTrace();
        }

    }