web-dev-qa-db-fra.com

Comment activer le bouton "Partager" dans l'application Android?

je veux ajouter le bouton "Partager" à mon application Android.

Comme ça

:

J'ai ajouté le bouton "Partager", mais le bouton n'est pas actif. Je clique, mais rien ne se passe.

Mon code dans MainActivity.Java:

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.share_menu, menu);
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.findItem(R.id.share_menu);
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return true;
}

{
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Android.content.Intent.EXTRA_TEXT, "Text");
    sharingIntent.putExtra(Android.content.Intent.EXTRA_SUBJECT, "Subject");
    startActivity(Intent.createChooser(sharingIntent, "Share using"));
}

Je souhaite partager du texte dans mon premier onglet (first_tab.xml) ou mon deuxième onglet (second_tab.xml).

Code dans l'onglet (xml) (si besoin):

<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@color/background_color"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >

<TextView
    Android:id="@+id/section_label1"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_centerHorizontal="true"
    Android:layout_centerVertical="true"
    Android:text="@string/text"
    Android:textColor="@color/text_color" />

<ImageView
    Android:id="@+id/imageView1"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_alignParentBottom="true"
    Android:layout_centerHorizontal="true"
    Android:src="@drawable/Sprite" />
102
Personal Jesus

Ajoutez un Button et en cliquant sur le Button ajoutez ce code:

Intent sharingIntent = new Intent(Android.content.Intent.ACTION_SEND); 
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(Android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(Android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

Liens utiles:

Pour le partage de base

pour personnalisation

276
Basavaraj Hampali

Créez un bouton avec un partage d'identifiant et ajoutez l'extrait de code suivant.

share.setOnClickListener(new View.OnClickListener() {             
    @Override
    public void onClick(View v) {

        Intent sharingIntent = new Intent(Android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "Your body here";
        String shareSub = "Your subject here";
        sharingIntent.putExtra(Android.content.Intent.EXTRA_SUBJECT, shareSub);
        sharingIntent.putExtra(Android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share using"));
    }
});

L'extrait de code ci-dessus ouvrira le sélecteur de partage lors d'une action de clic sur le bouton de partage. Cependant, remarque ... L'extrait de code de partage peut ne pas générer de très bons résultats à l'aide de l'émulateur. Pour obtenir des résultats réels, exécutez l'extrait de code sur le périphérique Android afin d'obtenir les résultats réels.

10
Daniel Nyamasyo

en kotlin:

val sharingIntent = Intent(Android.content.Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
val shareBody = "Application Link : https://play.google.com/store/apps/details?id=${App.context.getPackageName()}"
sharingIntent.putExtra(Android.content.Intent.EXTRA_SUBJECT, "App link")
sharingIntent.putExtra(Android.content.Intent.EXTRA_TEXT, shareBody)
startActivity(Intent.createChooser(sharingIntent, "Share App Link Via :"))
0
mhKarami