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
- Service Android में एक background component होता है, जो बिना UI के long-running operations को execute करने के लिए उपयोग किया जाता है।
- Services का उपयोग music playback, background data sync, location tracking, और network requests जैसे tasks के लिए किया जाता है।
- यह UI thread (Main Thread) को block किए बिना background में कार्य करता है, जिससे app का performance प्रभावित नहीं होता।
- Service को startService() method से start किया जाता है और stopService() से बंद किया जाता है।
- IntentService एक special प्रकार की service होती थी, जो background में tasks को sequentially execute करती थी, लेकिन अब deprecated हो चुकी है।
- JobIntentService और WorkManager को modern background task scheduling के लिए उपयोग किया जाता है।
- Services background में चलने के कारण battery consumption और performance optimization के लिए Android ने restrictions implement किए हैं, जैसे Doze Mode और Background Execution Limits।
- Foreground services को एक notification दिखाना अनिवार्य होता है, ताकि user को इसकी background activity के बारे में पता चल सके।
- सही तरीके से services implement करने से app का performance बेहतर होता है और user experience smooth बना रहता है।
Types of Services in Hindi
Android में तीन प्रकार की services होती हैं:
1. Foreground Service
- यह service user को actively दिखती है और एक ongoing notification के साथ चलती रहती है।
- इसका उपयोग music player, fitness tracking, और location tracking जैसी apps में किया जाता है।
- इसे startForeground() method से शुरू किया जाता है।
- Example: जब कोई music player चलता है, तो notification bar में controls दिखाई देते हैं।
2. Background Service
- यह बिना किसी direct user interaction के background में चलता है।
- Android 8.0 (Oreo) से इस पर restrictions लगाई गई हैं, जिससे battery optimization बेहतर हो सके।
- इसका उपयोग data syncing, auto-backup, और periodic updates के लिए किया जाता है।
- Example: Cloud storage app background में data upload करती है।
3. Bound Service
- यह किसी दूसरे component (जैसे Activity, BroadcastReceiver, या अन्य Services) से bind होकर चलता है।
- जब तक कोई component इससे connected रहता है, यह service active रहती है।
- इसे bindService() method से start किया जाता है और onBind() method implement करना जरूरी होता है।
- Example: Messenger apps में जब user call करता है, तो यह 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 करता है:
- START_STICKY – यदि service forcefully kill हो जाती है, तो restart हो जाती है।
- START_NOT_STICKY – यदि service kill हो जाती है, तो restart नहीं होगी।
- START_REDELIVER_INTENT – यदि service restart होती है, तो last intent फिर से भेजा जाएगा।
- onBind(Intent intent) – जब कोई component (जैसे activity) bound service से connect होता है, तो यह method call होती है और एक IBinder object return होता है।
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:
- Start करने के लिए → onCreate() → onStartCommand()
- Bind करने के लिए → onCreate() → onBind()
- Unbind होने पर → onUnbind() → onDestroy()
- Stop होने पर → onDestroy()
सही तरीके से service life cycle manage करने से memory leaks, battery drain और app crashes को रोका जा सकता है।
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