परिचय
Android में Implicit Intent का उपयोग करके, हम ACTION_SEND एक्शन का उपयोग करके अन्य एप्लिकेशन के साथ डेटा भेज सकते हैं। हमें Android मोबाइल के डिफॉल्ट चूज़र को खोलने के लिए Intent.createChooser() कॉल करना होगा ताकि डेटा भेजा जा सके। यह Android मोबाइल में समान या अलग हो सकता है।
यह उदाहरण दर्शाता है कि Android में ऐप डेटा को अन्य एप्लिकेशन के साथ कैसे साझा करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में लेआउट जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/layout"
android:gravity="center"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/data"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="www.tutorialspoint.com"
android:layout_height="wrap_content" />
</LinearLayout>
उपरोक्त कोड में, इसमें TextView है, जब उपयोगकर्ता TextView पर क्लिक करता है तो यह TextView डेटा को अन्य एप्लिकेशन के साथ भेजेगा।
चरण 3: MainActivity.java में कोड जोड़ें
package com.example.andy.myapplication;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public int counter;
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = findViewById(R.id.data);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_TEXT, textView.getText().toString());
startActivity(Intent.createChooser(i, "Share to"));
}
});
}
}
उपरोक्त कोड में TextView है, जब आप TextView पर क्लिक करते हैं, तो यह Intent का उपयोग करके डेटा भेजेगा।
चरण 4: Manifest.xml
Manifest.xml फ़ाइल में किसी बदलाव की आवश्यकता नहीं है।
ऐप चलाएं
मान लें कि आपने अपना वास्तविक Android मोबाइल डिवाइस कंप्यूटर से कनेक्ट किया है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन
पर क्लिक करें। अपने मोबाइल डिवाइस को विकल्प के रूप में चुनें और फिर अपना मोबाइल डिवाइस जांचें जो डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा:

यह प्रारंभिक स्क्रीन है जब आप tutorialspoint.com पर क्लिक करते हैं तो यह आपके Android डिवाइस से डिफॉल्ट चूज़र खोलेगा जैसा कि नीचे दिखाया गया है:

डेटा भेजने के लिए किसी भी एप्लिकेशन का चयन करें। या तो हम डेटा को एक व्यक्ति को भेज सकते हैं या अपनी वॉल पर पोस्ट कर सकते हैं, यह चूज़र एप्लिकेशन पर निर्भर करता है।