Welcome to

w3study.github.io




Topics

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, 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

java

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 करें

xml

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

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

Start Service:

java

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

Stop Service:

java

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 को रोका जा सकता है।

Services Lifecycle in Android


  • Broadcast Receivers in Hindi

  • Development of Android Applications Notes in Hindi


  • Request:

    हैलो दोस्तों! उम्मीद करता हूं आपको हमारा यह content/post पसंद आया होगा। अगर आपको हमारा ये content/post पसंद आई हो तो अपने दोस्तों के पास भी share करे। और अगर आपको कोई problem या कोई specific content हिन्दी में चाहिए है तो आप हमें नीचे दिए गए Email या whatsapp number के जरिए बता सकते है।

    अगर आप CCC/diploma/polytechnic/MCA/BCA etc कर रहे है तो ये website स्पेशली आपके लिए ही है, जो student हिंदी में पढ़ाई करते है।

    Contact Us

    Email: deepanshuranjan8057@gmail.com

    Whatsapp: +91 8057754706



    Follow Us

    Facebook Logo    Instagram Logo