2013-06-18 6 views
87

Ich möchte "Share" -Schaltfläche zu meiner Android App hinzufügen.Wie aktiviere ich die "Share" Taste in der Android App?

Wie die

:

hinzugefügt I "Teilen" -Schaltfläche, aber Taste ist nicht aktiv. Ich klicke, aber nichts passiert.

Mein Code in 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")); 
} 

Ich mag Text in meinem ersten Register (first_tab.xml) oder zweiten Reiter (second_tab.xml) teilen.

-Code in Register (xml) (Falls notwendig):

<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" /> 

Leider mein Englisch

+5

Um diese Art von Share-Schaltfläche hinzuzufügen, müssen Sie ActionBar/ActionBarSherlock verwenden und ShareProvider hinzufügen. – hardartcore

Antwort

235

ein In Button und auf Klick des Button diesen Code hinzu:

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")); 

Nützliche Links:

For basic sharing

For customization

+0

Fügen Sie die Schaltfläche wo? Ich habe bereits einen Menüpunkt mit dem 'share' Icon in meiner Aktionsleiste erstellt. – Si8

+2

auf Menüpunkt click event diesen Code hinzufügen –

+0

Hallo, In obiger Methode scheint es mehrere Anwendungen anzuzeigen. Ich möchte wissen, welche Anwendung für die Freigabe und nach der Freigabe verwendet wird, muss ich eine API aufrufen. Ist es möglich, zu überprüfen, welche Anwendung verwendet und auch API nach dem Teilen aufrufen? Danke ... – 135

2

Erstellen Sie eine Schaltfläche mit einer ID teilen und fügen Sie den folgenden Code-Schnipsel.

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

Das obige Code-Snippet öffnet den Share-Chooser bei Share-Button-Klick-Aktion. Beachten Sie jedoch, dass das Share-Code-Snippet unter Verwendung des Emulators möglicherweise keine sehr guten Ergebnisse liefert. Für tatsächliche Ergebnisse führen Sie das Code-Snippet auf Android-Gerät, um die tatsächlichen Ergebnisse zu erhalten.

Verwandte Themen