एंड्रॉइड में ऐसी सर्विस बनाना जिसे सिस्टम किल न करे
एंड्रॉइड में Service बैकग्राउंड ऑपरेशन करता है जो UI के साथ इंटरैक्ट किए बिना काम करता है और एक्टिविटी नष्ट होने के बाद भी चलता रहता है। यह उदाहरण दर्शाता है कि ऐसी सर्विस कैसे बनाएं जिसे सिस्टम आसानी से किल न करे, WakeLock और START_STICKY का उपयोग करके।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल संशोधित करें
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 लिया है, जिस पर क्लिक करके सर्विस शुरू और बंद की जाएगी।
चरण 3: MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्न कोड जोड़ें:
package com.example.myapplication;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isMyServiceRunning(MyService.class)) {
text.setText("Stopped");
stopService(new Intent(MainActivity.this, MyService.class));
} else {
text.setText("Started");
startService(new Intent(MainActivity.this, MyService.class));
}
}
});
}
private boolean isMyServiceRunning(Class> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
चरण 4: सर्विस क्लास बनाएं
पैकेज फोल्डर में MyService.java नाम से एक नई क्लास बनाएं और निम्न कोड जोड़ें:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
PowerManager pm;
@SuppressLint("InvalidWakeLockTag")
PowerManager.WakeLock wl;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("InvalidWakeLockTag")
@Override
public void onCreate() {
super.onCreate();
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyServiceTag");
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
wl.acquire();
// फोरग्राउंड सर्विस के लिए नोटिफिकेशन बनाएं (Android 8.0+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"service_channel", "My Service", NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, "service_channel")
.setContentTitle("Service Running")
.setContentText("Background service is active")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build();
startForeground(1, notification);
}
Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
wl.release();
Toast.makeText(this, "Service destroyed by user.", Toast.LENGTH_LONG).show();
}
}
चरण 5: AndroidManifest.xml में परमिशन और सर्विस डिक्लेयर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<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>
मुख्य बिंदु
- WakeLock:
PowerManager.SCREEN_DIM_WAKE_LOCKका उपयोग करके CPU को चालू रखता है - START_STICKY: सिस्टम द्वारा किल होने पर सर्विस को रीस्टार्ट करता है
- Foreground Service: Android 8.0+ में फोरग्राउंड सर्विस के रूप में चलने के लिए नोटिफिकेशन आवश्यक है
- WAKE_LOCK परमिशन: वेक लॉक प्राप्त करने के लिए जरूरी
ऐप चलाएं
अपने वास्तविक एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फ़ाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। TextView पर क्लिक करें - सर्विस शुरू होगी। फिर से क्लिक करने पर सर्विस बंद होगी।
नोट: आधुनिक एंड्रॉइड संस्करणों (8.0+) में बैकग्राउंड सर्विस प्रतिबंधों के कारण, फोरग्राउंड सर्विस का उपयोग करना अनिवार्य है। साथ ही, अत्यधिक बैटरी उपयोग से बचने के लिए WakeLock का विवेकपूर्ण उपयोग करें।