Android में फोरग्राउंड सर्विस के साथ बैकग्राउंड एक्सेक्यूशन
यह उदाहरण दर्शाता है कि START_STICKY और onTaskRemoved का उपयोग करके एक सर्विस को बैकग्राउंड में कैसे चलाया जाए जो ऐप किल होने पर भी रीस्टार्ट होती है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="बैकग्राउंड सर्विस शुरू करें"
android:textSize="16sp"
android:textStyle="bold"/>
</RelativeLayout>
चरण 3: सर्विस क्लास (MyService.java)
package app.com.sample;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// सर्विस शुरू होने पर टोस्ट दिखाएं
Toast.makeText(getApplicationContext(),
"बैकग्राउंड में सर्विस चल रही है", Toast.LENGTH_SHORT).show();
// START_STICKY: सिस्टम द्वारा किल होने पर रीस्टार्ट करता है
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// बाइंडेड सर्विस नहीं - null लौटाएं
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
// ऐप टास्क मैनेजर से स्वाइप करके हटाए जाने पर
Intent restartIntent = new Intent(getApplicationContext(), this.getClass());
restartIntent.setPackage(getPackageName());
startService(restartIntent);
super.onTaskRemoved(rootIntent);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "सर्विस नष्ट हुई", Toast.LENGTH_SHORT).show();
}
}
चरण 4: MainActivity (src/MainActivity.java)
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.button);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// सर्विस शुरू करें
Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
startService(serviceIntent);
}
});
}
}
चरण 5: AndroidManifest.xml - सर्विस घोषणा
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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">
<!-- सर्विस घोषणा -->
<service
android:name=".MyService"
android:enabled="true"
android:exported="false">
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
सर्विस लाइफसाइकल और फ्लैग्स
| रिटर्न वैल्यू | व्यवहार |
|---|---|
START_STICKY |
किल होने पर रीस्टार्ट, इंटेंट null हो सकता है |
START_NOT_STICKY |
किल होने पर रीस्टार्ट नहीं (डिफ़ॉल्ट) |
START_REDELIVER_INTENT |
किल होने पर रीस्टार्ट + अंतिम इंटेंट रीडिलीवर |
आधुनिक Android (API 26+) के लिए फोरग्राउंड सर्विस
// सर्विस में onCreate या onStartCommand में:
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("बैकग्राउंड सर्विस")
.setContentText("चल रही है...")
.setSmallIcon(R.drawable.ic_notification)
.build();
startForeground(NOTIFICATION_ID, notification);
ऐप चलाएं
ऐप लॉन्च करें, "बैकग्राउंड सर्विस शुरू करें" दबाएं, फिर ऐप बंद करें - सर्विस चलती रहेगी (पुराने Android पर)।

