Skip to main content

Android Intents

Android Intents


  • Introduction to android intents in Hindi
  • Types of android intents in Hindi
  • Android intent filter in Hindi

Introduction to Android Intents

Intents का मतलब होता है आप क्या करना चाहते है। 


जैसे की
  • में contacts list देखना चाहता हूँ। 
  • में कैमरा से फोटो क्लिक करना चाहता हूँ। 
  • में messages शो करना चाहता हूँ। 


आप क्या करना चाहते है ये आप इंटेंट क्रिएट करके एक मैसेज के द्वारा बताते है।

Intents messages होते है जो android components(Activity, Service, Broadcast receiver, Content provider ) एक दूसरे को भेजते है। एक component किसी दूसरे component को किसी functionality के लिए request करता है। ये request messages के द्वारा की जाती है जिन्हे intents कहते है। Intents asynchronous messages होते है क्योंकि android components बिना wait किये एक दूसरे को ये messages send करते है। 


Intents की मदद से आप same एप्लीकेशन में दूसरे कंपोनेंट्स के साथ इंटरैक्ट कर सकते है और दूसरी एप्लीकेशन के कंपोनेंट्स से भी आप intents के द्वारा इंटरैक्ट कर सकते है। उदाहरण के लिए कोई activity दूसरी activity को स्टार्ट कर सकती है जो की contacts को शो करेगी।



how-intents-works-in-hindi

    
android.content.Intent क्लास android system में है जिसके आप ऑब्जेक्ट क्रिएट करते है और उन ऑब्जेक्ट में आप क्या करना चाहते है (ACTION) और आपका टारगेट कॉम्पोनेन्ट क्या है(URI of Target Component) आप आर्गुमेंट की तरह पास करते है।





किसी activity को स्टार्ट करने के लिए सबसे पहले आपको intent क्रिएट करना होगा। उसके बाद आप startActivity() मेथड कॉल करके उसमे intent को आर्गुमेंट की तरह पास करेंगे इससे एक एक्टिविटी स्टार्ट हो जाएगी।



Intent contactsIntent = new Intent(Intent.ACTION_PICK,Uri.parse("Content://contacts/people"));
startActivity(contactsIntent);



इस example में पहले मेने एक intent क्रिएट किया है जिसे मेने contactsIntent नाम दिया है। Intent क्रिएट करते समय हम डिफाइन करते है की हम क्या करना चाहते है। जैसे की मेने डिफाइन किया है की में कॉन्टेक्ट्स लिस्ट देखना चाहता हूँ। इसके बाद मेने startActivity() मेथड कॉल किया है जिसमे मेने उस इंटेंट को पास किया है। ये मेथड एक एक्टिविटी को स्टार्ट करता है जो कॉन्टेक्ट्स लिस्ट को शो करती है।

जैसे हमने एक एक्टिविटी स्टार्ट की है उसी प्रकार आप एक Service भी स्टार्ट कर सकते है। इसके लिए आपको startService() मेथड कॉल करके उसमे इंटेंट पास करना होगा।



Intent contactsIntent = new Intent(Intent.ACTION_PICK,Uri.parse("Content://contacts/people"));
startService (contactsIntent);


Broadcast messages ग्लोबल इवेंट्स होते है जो सारे android system में जाते है। एक broadcast receiver ऐसे messages के लिए register करवा सकता है। जब इवेंट generate होता है तो उसे notification message मिल जाता है।





Types of Intents

Intents 2 तरह के होते है Implicit intent और Explicit intent। 

Implicit Intent

जैसे की आपने एक intent क्रिएट किया जो की एक web address को ओपन करता है। लेकिन android system में एक से ज्यादा browsers हो सकते है। यदि सिर्फ एक target (Browser ) है तो directly वही open हो जायेगा नहीं तो  जितने browsers है उनकी लिस्ट show हो जाएगी जिसमे से यूज़र सेलेक्ट कर सकता हे और उसी टारगेट के साथ intent execute हो जायेगा।


Explicit Intent

Explicit intents में आप पहले ही डिफाइन कर देते है की intent किस टारगेट के साथ execute होना है। जैसे की आपने एक intent क्रिएट किया जो एक वेब एड्रेस ओपन करता है तो आप इसमें पहले ही डिफाइन कर देते है की ये इंटेंट Google Chrome के साथ execute होना है। इसमें हम टारगेट कॉम्पोनेन्ट पहले से ही डिफाइन कर देते है।



Intent Filters

आपने एक application क्रिएट की है जो की एक वेब browser है। यूजर किसी pdf reader में किसी वेब एड्रेस की लिंक पर क्लिक करता है तो एक इंटेंट generate होता है। यदि आप चाहते है की आपके ब्राउज़र में ये वेब एड्रेस ओपन हो तो आपको इस intent से रिलेटेड intent filter अपनी application के component के लिए declare करना होगा।

जब भी कोई intent generate होता है तो सभी registered एप्लीकेशन कंपोनेंट्स को message मिलता है और वो उसके according एक्शन लेते है। यदि आप चाहते है की आपकी एप्लीकेशन intents को receive कर सके तो आपको अपनी एप्लीकेशन को उस intent के लिए register करवाना होगा। Intents के लिए register करने के लिए आपको intent-filter डिक्लेअर करना होता है।


intent filters आप AndroidManifest.xml में क्रिएट करते है।



<intent-filter>

</intent-filter>






Intent filters के 3 element tags  होते है। 
  1. <action >  -   इसमें आप ये डिफाइन करते है की आप किस तरह के action के लिए intent filter डिफाइन कर रहे है। एक ही एप्लीकेशन कई intents generate कर सकती है। लेकिन आप उनको एक्शन के आधार पे अलग अलग पहचान सकते है। Action डिफाइन करने के लिए andorid : name attribute use किया जाता है।    
  2. <data >  -  इसमें आप डिफाइन करते है की आप किस तरह के डेटा के लिए intent filter डिफाइन कर रहे है। ये basically components का URI होता है। डेटा को define करने के लिए android : scheme attribute use किया जाता है।    
  3. <category >  -  इसमें आप intent की category define करते है। आप चाहे तो default-category भी यूज़ कर सकते है। Category डिफाइन करने के लिए android : name attribute यूज़ किया जाता है।          

जो एक्शन आप intent क्रिएट करते समय डिफाइन करते है वही action आप action tag में देते है। और डेटा tag में आप Component का URI देते है।

Explicit intents में हम target component खुद डिफाइन कर देते है। लेकिन implicit intents में target component डिफाइन नहीं होता है। इसलिए जिन application को implicit intents receive करने होते है वो intent filters डिक्लेअर करती है। जब intent क्रिएट होता है तो उन्हें notification message मिल जाता है। 

Comments

Popular posts from this blog

Android Services

Android Services Introduction to  android services in Hindi  Declaring  services in Hindi Creating a  android service in Hindi Introduction to Android Services इससे पहले की आप जाने की service क्या होती है आइये जानते है की service क्या नहीं होती है - एक service separate प्रोसेस नहीं होती है। ये आपकी एप्लीकेशन का ही पार्ट होती है।  एक service thread नहीं होती है।  आइये अब देखते है service क्या होती है - Service एक facility होती है जिससे आप android system को बता सकते है की आप कोई काम background में करते रहना चाहते है।  Service एक facility होती है जिससे एक एप्लीकेशन अपनी कुछ functionality दूसरी applications को expose कर सकती है।    हर service क्लास का <service> declaration AndroidManifest.xml file में होना चाहिए। Services को आप context.startService() और context.bindService() मेथड्स से स्टार्ट कर सकते है।            जब service component क्रिएट होता है तो android syst...

Android Activity

Android Activity Activity life cycle in Hindi Starting an  android activity in Hindi Creating a new activity in Hindi Destroying an  android activity in Hindi Introduction to Activity आप की application में एक से ज्यादा activity हो सकती है। जब आप इन activities के through navigate करते है, यानि एक activity से दूसरी activity में जाते है तो activity अपनी life cycle की different stages में जाती है। उदाहरण के लिए जब आपकी activity start होती है तो ये foreground में होती है और user का पूरा focus इस पर होता है। इस process के दौरान  android  system कुछ life cycle methods को call करता है। यदि user कोई दूसरी activity start करता है तो android system उस activity के life cycle methods को call करता है और आपकी पहले वाली activity background में चली जाती है। life cycle methods में आप decide कर सकते है की जब user जाता है या वापस आता है तो आपकी activity कैसे behave करेगी।  उदाहरण के लिए यदि आप एक वीडियो player बना रहे है तो जब user किसी दूसरी activity पर जाये तो...

Android Spinners

Android Spinners  Introduction to  android spinners in Hindi Defining  android spinners in Hindi Creating spinner items array in Hindi Responding to spinner selections in Hindi    Introduction to Android Spinners Android spinners आपकी एप्लीकेशन में एक drop down menu होती है। इस drop down menu में बहुत से items होते है जिन्हे user choices कहते है। जब user spinner पर क्लिक करता है तो ये items शो होते है। यूज़र अपने लिए appropriate choice choose करता है। By default spinner में सबसे top वाला element शो होता है। Spinners को XML layout file में क्रिएट किया जाता है। और items की लिस्ट string resource file में क्रिएट की जाती है। जिसमे आप एक string array क्रिएट करते है।  Spinner और items को जोड़ने के लिए एक array adapter क्रिएट किया जाता है। इसके बाद selection events को respond करने के लिए OnItemSelectedListener interface को  इम्प्लीमेंट किया जाता है।  Defining Android Spinners Android spinners को आप XML l...