Theory
अपने App से आगे पहुँचना
FestConnect Mobile दो चीज़ें चाहता है जो यह अकेले नहीं कर सकता: fest की WEBSITE खोलना (एक browser चाहिए) और users को WhatsApp या email पर एक event SHARE करने देना (वे apps चाहिए)। आपके app में एक browser या messaging client होना नहीं चाहिए, और होना भी नहीं चाहिए।
इसकी बजाय, Android आपके app को system से ASK करने देता है: 'कोई यह URL खोले' या 'कोई यह text share करे', और system एक app ढूँढता है जो कर सके। यह implicit intent है, और यही तरीका है जिससे apps cooperate करते हैं। यह lesson URLs खोलना और content share करना cover करता है, दो implicit-intent patterns जिन्हें syllabus नाम देता है, FestConnect Mobile को पूरे device में plug-in करने देते हुए।
Theory
Implicit Intents: Action Describe कीजिए
एक implicit intent एक specific app नाम दिए WITHOUT एक action (और optionally data) specify करता है। Android एक suitable handler ढूँढता है।
System कैसे जानता है कौन से apps एक action handle कर सकते हैं? Apps अपने manifest में intent filters declare करते हैं, वे actions advertise करते हुए जो वे support करते हैं (एक browser advertise करता है यह web URLs के लिए ACTION_VIEW handle कर सकता है; WhatsApp text के लिए ACTION_SEND advertise करता है)। जब आप एक implicit intent fire करते हैं, Android इसे इन filters से match करता है और matching apps offer करता है।
तो आपका app INTENT express करता है ('यह view करो', 'यह send करो') और system इसे एक capable app से connect करता है, आपके app को जानने या care करने के बिना कि user के पास कौन से apps installed हैं। यह loose coupling एक core Android design strength है।
Practical
एक URL खोलना, और एक Event Share करना
// OPEN A URL: ACTION_VIEW with a web Uri -> opens in a browser
fun openFestWebsite() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://fest.example"))
startActivity(intent) // system finds a browser to handle it
}
// SHARE text to any app (WhatsApp, email, etc.)
fun shareEvent(name: String) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Join me for $name at TechnoUtsav!")
}
// createChooser shows the app picker ("Share via ...")
startActivity(Intent.createChooser(intent, "Share event"))
}
Theory
ACTION_VIEW, ACTION_SEND, और Chooser
Syllabus नाम देता वे दो patterns:
- एक URL खोलना:
Intent(Intent.ACTION_VIEW, Uri.parse(url))फिरstartActivity। एक web URI वाला ACTION_VIEW system से इसे VIEW करने कहता है, और एक browser (या एक matching app) इसे खोलता है - Content Share करना:
Intent(Intent.ACTION_SEND)एकtypeके साथ (text के लिए"text/plain", एक image के लिए"image/*") औरEXTRA_TEXTमें content (या एक media URI)। यह उस type accept करने वाले किसी भी app को share करता है
Sharing के लिए, 'Share via...' PICKER दिखाने के लिए intent को Intent.createChooser(intent, "Share event") में wrap कीजिए, user को कौन सा app चुनना है choose करने देते हुए। Chooser के बिना, Android एक default pick कर सकता है। Chooser वह familiar share sheet है जो आप Android apps के across देखते हैं: आपका app इसे एक implicit intent से trigger करता है।
Quiz
FestConnect Mobile users को जो भी messaging app वे prefer करें उसमें एक event share करने देना चाहता है। कौन सा approach?
- WhatsApp detect कीजिए और directly इसे एक explicit intent से launch कीजिए
- एक implicit intent इस्तेमाल कीजिए (EXTRA_TEXT वाला ACTION_SEND), optionally createChooser में wrapped, तो system सारे capable apps offer करता है
- FestConnect के अंदर एक messaging feature build कीजिए
- एक app से share करना impossible है
Show the answer
एक implicit intent इस्तेमाल कीजिए (EXTRA_TEXT वाला ACTION_SEND), optionally createChooser में wrapped, तो system सारे capable apps offer करता है
User जो भी app prefer करे उसमें share करना exactly एक IMPLICIT intent का job है: EXTRA_TEXT में content (और एक type) वाला ACTION_SEND, जो system हर उस app को offer करता है जो sharing handle कर सकता है, और createChooser picker दिखाता है ताकि user एक select करे। Option A (explicitly WhatsApp target करना) brittle और wrong-spirited है: यह assume करता है WhatsApp installed है और user के दूसरे apps ignore करता है; implicit intents एक specific app नाम देने से बचते हैं। Option C purposelessly messaging reinvent करता है, आपके app को existing apps के साथ COOPERATE करना चाहिए, उन्हें duplicate नहीं। Option D false है। Design lesson: एक specific app की बजाय एक CAPABILITY (share, view, call) invoke करने के लिए, एक implicit intent इस्तेमाल कीजिए और system को आपको जो भी handle करता है उससे connect करने दीजिए।
Think first
Directly WhatsApp Target क्यों न करें?
Sharing के लिए specifically WhatsApp launch करना simpler लग सकता है। Implicit-intent (let-the-user-choose) approach बेहतर क्यों है? फिर tap कीजिए।
Show the answer
कई reasons। (1) HAR किसी के पास WhatsApp नहीं है; एक user Telegram, email, SMS, या Instagram prefer कर सकता है, और WhatsApp को hard-target करना fail होता है या उन्हें exclude करता है। (2) implicit-intent + chooser approach USER CHOICE को respect करता है: share sheet उनके सारे capable apps दिखाता है, और वे choose करते हैं। (3) यह ROBUST है: आपका app किसी specific app के installed होने या उसके internal details पर depend नहीं करता (जो बदल सकते हैं और आपको break कर सकते हैं)। (4) यह Android की loose coupling की DESIGN philosophy follow करता है: apps capabilities advertise करते हैं (intent filters के through) और system इन्हें connect करता है, तो कोई भी capable app काम करता है आपके app को इसके बारे में जाने बिना। एक app target करना fragile, presumptuous, और limiting है; ACTION describe करना और system को handlers offer करने देना flexible, respectful, और future-proof है। Capability invoke कीजिए, app नहीं।
Watch out
Implicit-Intent Traps
एक Specific App Hard-Target करना: action describe करना और system को handlers offer करने देना prefer कीजिए।
कोई Handler Available नहीं: अगर कोई app एक implicit intent handle नहीं कर सकता, startActivity crash कर सकता है; resolveActivity से check कीजिए या exception handle कीजिए।
ACTION_SEND पर ग़लत Type: content के हिसाब से type text/plain, image/*, वग़ैरह set कीजिए, नहीं तो apps इसे accept नहीं करेंगे।
createChooser भूलना: इसके बिना, Android user को choose करने देने की बजाय एक default launch कर सकता है।
एक Browser के Exist करने का Assume करना: usually safe है, पर robust apps फिर भी no-handler case handle करते हैं।
Theory
App Cooperate करता है; अब यह Remember करता है
FestConnect Mobile URLs खोल सकता है और events share कर सकता है, device के दूसरे apps में plug-in करते हुए। पर यह closed होने पर अभी भी अपना खुद का data भूल जाता है। अगला lesson इसे SQLite (Android का built-in database) से PERSISTENT storage देता है, full CRUD के साथ, ताकि registrations sessions के बीच survive करें, वह BCA205, BCA303 और BCA504 में आपने जो databases build किए उनका mobile echo। फिर location और camera device capabilities add करते हैं।
Summary
Key takeaways
- एक implicit intent एक app नाम दिए बिना एक ACTION (और optionally data) specify करता है; system एक handler ढूँढता है।
- Apps अपने manifest में intent filters से advertise करते हैं वे क्या handle करते हैं; Android आपके intent को इनसे match करता है।
- एक URL खोलिए: Intent(Intent.ACTION_VIEW, Uri.parse(url)) + startActivity, एक browser में खुलता है।
- Content share कीजिए: Intent(Intent.ACTION_SEND) एक type और EXTRA_TEXT (या media URI) के साथ।
- 'Share via...' app picker दिखाने के लिए sharing को Intent.createChooser(intent, title) में wrap कीजिए।
- एक CAPABILITY (share, view) invoke करने के लिए implicit intents इस्तेमाल कीजिए, एक specific app नहीं: flexible और user-respecting।
- Memory hook: implicit intent action describe करता है, system apps offer करता है, createChooser user को choose करने देता है।