2013-06-18 8 views
87

अपने 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

में 'share' आइकन के साथ एक मेनू आइटम बनाया इस कोड –

+0

जोड़ने हैलो, उपरोक्त विधि में यह एक से अधिक आवेदन प्रदर्शित करने के लिए लगता है। मुझे पता है कि जो आवेदन वितरण के लिए उपयोग और पूर्ण साझा करने के बाद मैं एक एपीआई कॉल करनी होगी चाहते हैं। यह जांचना संभव है कि किस एप्लिकेशन का उपयोग किया गया था और साझा करने के बाद एपीआई को कैसे कॉल किया जाए? धन्यवाद ... – 135

2

एक आईडी हिस्सेदारी के साथ एक बटन बनाएँ और निम्नलिखित कोड का टुकड़ा जोड़ें।

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

ऊपर कोड स्निपेट साझा करें बटन क्लिक कार्रवाई पर शेयर चयनकर्ता खुल जाएगा। हालांकि, ध्यान दें ... शेयर कोड स्निपेट एमुलेटर का उपयोग नहीं कर सकता है उत्पादन बहुत अच्छे परिणाम। वास्तविक परिणामों के लिए, असली परिणाम प्राप्त करने के लिए एंड्रॉइड डिवाइस पर कोड स्निपेट चलाएं।

संबंधित मुद्दे