Menu

Related Topics

HTML CSS JavaScript PHP Node.js React.js C C++ Go Rust Swift Python Java Excel MS Word PowerPoint
Search

Overview of Services, Types of Services, Implementing a service, 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 को रोका जा सकता है।



Page Views
0
Visit ID
Not tracked yet
Visit Time
-
Time on Page
0 seconds

Request


Leave a Comment


INITIALIZING...


INITIALIZING...


Quizzes in Hindi

GK Online Quiz in Hindi 10 pages
HTML Online Quiz in Hindi 10 pages
CSS Online Quiz in Hindi 10 pages
JavaScript Online Quiz in Hindi 10 pages
CCC Online Quiz in Hindi 10 pages
History Online Quiz in Hindi 10 pages



Comments Total: 0 comments

📰 Latest Articles

Total: 0 Articles

Featured Products

0 Products 0 Loaded 0 Remaining

All Products Loaded!

You've reached the end of our collection.

Contact Us Form
C