deepeweb.github.io

Overview of Services in Hindi, Types of Services in Hindi, Implementing a service in Hindi, Service Lifecycle in Hindi

Overview of Services in Hindi

Types of Services in Hindi

Android में तीन प्रकार की services होती हैं:

1. Foreground Service

2. Background Service

3. Bound Service

Android में Service Implement करने का तरीका

1. Simple Background Service Implementation

public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyService", "Service Created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService", "Service Started");

        // Background task को एक अलग thread पर चलाएं
        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                Log.d("MyService", "Running... " + i);
                try {
                    Thread.sleep(1000); // 1 सेकंड की देरी
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            stopSelf(); // Task पूरा होने के बाद service को रोकें
        }).start();

        return START_STICKY; // Service को restart करने की अनुमति देता है
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null; // Unbound service के लिए null return करें
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyService", "Service Destroyed");
    }
}

2. Service को Manifest में Declare करें

<service android:name=".MyService" />

3. Service Start और Stop करने का तरीका

Start Service:

Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);

Stop Service:

Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);

Service Lifecycle in Hindi

onCreate() – जब service पहली बार start होती है, तो यह method call होती है। इसमें initialization process किया जाता है।

onStartCommand(Intent intent, int flags, int startId) – जब service start होती है, तो यह method call होती है। इसमें background task execute किए जाते हैं। यह method तीन return modes को support करता है:

onUnbind(Intent intent) – जब सभी bound components service से disconnect हो जाते हैं, तो यह method call होती है।

onRebind(Intent intent) – जब कोई पहले से unbind की गई service फिर से bind होती है, तो यह method call होती है।

onDestroy() – जब service stop होती है या system द्वारा destroy की जाती है, तो यह method call होती है और cleanup process किया जाता है।

Service Life Cycle Flow:

सही तरीके से service life cycle manage करने से memory leaks, battery drain और app crashes को रोका जा सकता है।




Request

अगर आपको यह article useful या interesting लगा हो, तो please इसे अपने dosto aur family ke साथ जरूर share करें। आपका एक छोटा सा कदम हमें और अच्छा content बनाने के लिए motivate करता है। Thank you!

ध्यान दें कि इस page पर आपको कुछ ads भी देखने को मिल सकते हैं। इसके लिए हम आपसे माफी चाहते हैं। हम इस content को तैयार करने में काफी मेहनत और time लगाते हैं, ताकि आपको valuable जानकारी मिल सके। इन्हीं ads की मदद से हम ये काम continue कर पाते हैं।

आपके support और understanding के लिए दिल से धन्यवाद।





Ad
Ad


Ad
Ad

Follow Us

Facebook Logo    Instagram Logo