उदाहरण में जाने से पहले, हमें पता होना चाहिए कि Android में IntentService क्या है। IntentService बैकग्राउंड ऑपरेशन को असिंक्रोनस रूप से करता है। जब उपयोगकर्ता एक्टिविटी से startService() कॉल करता है, तो यह प्रत्येक रिक्वेस्ट के लिए इंस्टेंस नहीं बनाता। सर्विस क्लास में कुछ एक्शन पूरा होने के बाद यह सर्विस को रोक देता है या फिर हमें stopSelf() का उपयोग करके सर्विस को रोकना पड़ता है।
IntentService से शेयर इंटेंट भेजने का उदाहरण
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और आवश्यक विवरण भरें।
चरण 2 - लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
उपरोक्त कोड में, हमने TextView लिया है। जब उपयोगकर्ता TextView पर क्लिक करता है, तो यह Android OS से डिफॉल्ट शेयर डायलॉग खोलेगा।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener(v ->
startService(new Intent(MainActivity.this, MyService.class)));
}
}
चरण 4 - Service क्लास बनाएं
MyService.java नामक क्लास फाइल बनाएं और निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends IntentService {
public static volatile boolean shouldStop = false;
public MyService() {
super(MyService.class.getSimpleName());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Tutorialspoint.com");
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(sharingIntent, "Sharing"));
if (shouldStop) {
stopSelf();
return;
}
}
}
सर्विस रोकने के लिए, सर्विस क्लास में निम्नलिखित कोड उपयोग करें:
stopSelf();
चरण 5 - Manifest में कोड जोड़ें
AndroidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"/>
</application>
</manifest>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। TextView पर क्लिक करने पर, मोबाइल OS से डिफॉल्ट शेयर डायलॉग दिखेगा।
महत्वपूर्ण नोट: Android 8.0 (API 26) से, बैकग्राउंड से एक्टिविटी शुरू करने पर प्रतिबंध है।
FLAG_ACTIVITY_NEW_TASKफ्लैग की आवश्यकता होती है। आधुनिक ऐप्स के लिएWorkManagerयाForeground Serviceका उपयोग करने पर विचार करें।