Как создать кнопку поделиться в Android с помощью намерения? - PullRequest
0 голосов
/ 13 июня 2019

В моем приложении есть кнопка «Поделиться», которая делится ссылкой на приложение в магазине Play.Я новичок в методе намерений и продолжаю получать эту ошибку:

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 ]

3 голосов
/ 13 июня 2019

В пункте меню / нажатии кнопки вызовите следующий метод.

private void shareAppLink(){
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_VIEW);
    shareIntent.setData(Uri.parse("Link to app store"));
    startActivity(shareIntent);
}
0 голосов
/ 13 июня 2019

Вот пример кода:

     <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();
        }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...