2013-06-18 131 views
87

我想添加「分享」按鈕到我的Android應用程序。如何激活android應用程序中的「共享」按鈕?

就像那個

:

我補充說: 「分享」 按鈕,但按鈕未激活。我點擊,但沒有任何反應。

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

我想在我的第一個選項卡(first_tab.xml)或第二個選項卡(second_tab.xml)共享文本。

代碼的標籤(XML)(如果需要):

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

對不起我的英語

+5

要添加這種共享按鈕,您需要使用ActionBar/ActionBarSherlock並添加ShareProvider。 – hardartcore

回答

235

添加Button並在Button的點擊添加以下代碼:

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

有用鏈接:

For basic sharing

For customization

+0

添加按鈕在哪裏?我已經創建了一個菜單項,在我的操作欄中有「共享」圖標 – Si8

+2

對菜單項單擊事件添加此代碼 –

+0

你好, 在上面的方法中,它似乎顯示多個應用程序。 我想知道哪個應用程序用於共享和共享完成後,我必須調用一個API。 是否可以檢查使用哪個應用程序以及共享後如何調用API? 謝謝... – 135

2

創建一個帶有ID共享按鈕,添加以下代碼段。

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

上面的代碼片段將打開共享按鈕單擊操作上的共享選擇器。 但是,請注意...共享代碼片段可能不會使用仿真器輸出非常好的結果。要獲得實際結果,請運行android設備上的代碼片段以獲取真實結果。

相關問題