एंड्रॉइड ऐप में "शेयर" बटन को कैसे सक्रिय करें?


109

मैं अपने एंड्रॉइड ऐप में "शेयर" बटन जोड़ना चाहता हूं।

उसके जैसा

:

मैंने "शेयर" बटन जोड़ा, लेकिन बटन सक्रिय नहीं है। मैं क्लिक करता हूं, लेकिन कुछ नहीं होता है।

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
इस तरह के Share बटन को जोड़ने के लिए आपको ActionBar / ActionBarSherlock का उपयोग करना होगा और ShareProvider को जोड़ना होगा।
h4rd4r7c0r3

जवाबों:


301

जोड़ें 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"));

उपयोगी कड़ियाँ:

बेसिक शेयरिंग के लिए

अनुकूलन के लिए


बटन कहां लगाएं? मैंने पहले ही shareअपने एक्शन बार में आइकन के साथ एक मेनू आइटम बनाया है
Si8

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

इसे code.tutsplus.com/tutorials/… से कॉपी किया गया है ।
कूलमाइंड

फेसबुक को छोड़कर मेरे लिए ठीक काम करता है। यह दुर्भाग्य से वहाँ कुछ भी नहीं दिखाता है।
एवाग्गेलोस मंसोकिस

छवि कैसे जोड़ें? क्या आप मुझे सुझाव दे सकते हैं ??
तस्नुवा ओशिन

13

एक आईडी शेयर के साथ एक बटन बनाएं और निम्नलिखित कोड स्निपेट जोड़ें।

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

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


4

कोटलिन में:

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 :"))
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.