2013-06-18 10 views
87

私のアンドロイドアプリに「共有」ボタンを追加します。そのアンドロイドアプリで「共有」ボタンをアクティブにするにはどうすればいいですか?

:

同様

私は、 "共有" ボタンを追加しましたが、ボタンがアクティブではありません。私はクリックしますが、何も起こりません。

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)または2番目のタブ(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

この種のShareボタンを追加するには、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

ボタンをどこに追加しますか?私はすでにアクションバーに 'share'アイコンを持つメニュー項目を作成しました。 – Si8

+2

メニュー項目のクリックイベントにこのコードを追加してください。 –

+0

こんにちは、 上記の方法では、複数のアプリケーションを表示するようです。 どのアプリケーションが共有に使用されているのかを知りたいのですが、共有が完了した後、1つの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")); 
    } 
}); 

上記のコードスニペットは、共有ボタンのクリック操作で共有選択を開きます。 ただし、共有コードスニペットは、エミュレータを使用して非常に良い結果を出力しないことがあります。実際の結果を得るには、アンドロイドデバイスでコードスニペットを実行し、実際の結果を取得します。

関連する問題