Android Intent क्या है? (Full Guide in Hindi)
Types • Components • Flags • Common Actions • Android Studio Code Examples
Intent in Android: आसान शब्दों में
Intent Android का एक messaging object है, जो components (Activities, Services, Broadcast Receivers) के बीच communication करवाता है। सरल भाषा में—Intent का मतलब है “कुछ करने का इरादा”, जैसे दूसरी Activity खोलना, किसी app को data भेजना, या system feature (Dialer/Camera) launch करना।
Table of Contents
Intent के Types
1) Explicit Intent
किसी specific component (जैसे आपकी ही app की Activity) को target करता है। Target class का नाम देते हैं।
2) Implicit Intent
किसी specific component का नाम नहीं; सिर्फ Action/Data देते हैं। System उन apps की list दिखाता/चुनता है जो उस action को handle कर सकें (Chooser के ज़रिये)।
3) PendingIntent
Future में किसी और app/system (जैसे Notification, AlarmManager) के द्वारा execute होने वाला “tokenized” intent। Security-boundary respect करता है।
4) Sticky Intent (Broadcast)
पुराना mechanism जिसमें broadcast “टिका” रहता था ताकि बाद में register हुए receivers को भी data मिल सके। अब deprecated/avoid—आधुनिक apps में प्रयोग न करें।
sendBroadcast()
से भेजते हैं और BroadcastReceiver
receive करता है।Intent के Components (Fields)
- Action — क्या करना है (जैसे
Intent.ACTION_VIEW
,ACTION_SEND
) - Data (URI) — किस data पर action (जैसे
tel:
,geo:
,content:
,http:
) - Category — अतिरिक्त info (जैसे
CATEGORY_BROWSABLE
) - Extras — key–value pairs में data (
putExtra()
,get...
) - Flags — launch behavior control (task stack आदि)
- Component — explicit target (package/class)
Quick Examples (Java)
Explicit: Activity A → Activity B
// A → B
Intent i = new Intent(A.this, B.class);
i.putExtra("username", "Rehan");
startActivity(i);
// B.java
String name = getIntent().getStringExtra("username");
// textView.setText("Hello " + name);
Implicit: Dialer खोलना
Intent dial = new Intent(Intent.ACTION_DIAL);
dial.setData(Uri.parse("tel:9876543210"));
startActivity(dial);
Implicit: Web URL खोलना
Intent web = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
startActivity(web);
Share Text (Chooser)
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.putExtra(Intent.EXTRA_TEXT, "Hello from my app!");
startActivity(Intent.createChooser(send, "Share via"));
PendingIntent with Notification
Intent open = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(
this, 0, open, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
// इस PendingIntent को Notification में contentIntent के रूप में सेट करें।
Kotlin Snippets
Explicit
val i = Intent(this, SecondActivity::class.java)
i.putExtra("id", 42)
startActivity(i)
Implicit (Maps)
val geo = Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=Jaipur"))
startActivity(geo)
Share Image
val send = Intent(Intent.ACTION_SEND).apply {
type = "image/*"
putExtra(Intent.EXTRA_STREAM, imageUri) // content:// URI via FileProvider
}
startActivity(Intent.createChooser(send, "Share image via"))
Common Intent Flags (संक्षेप में)
FLAG_ACTIVITY_NEW_TASK
— नई task में activity शुरू करें (अक्सर non-activity context से).FLAG_ACTIVITY_CLEAR_TOP
— अगर target ऊपर stack में है, उससे ऊपर की activities हटाएँ और उसे पुनः deliver करें।FLAG_ACTIVITY_SINGLE_TOP
— अगर top पर वही activity है, तो नया instance न बनाए;onNewIntent()
पाएं।FLAG_GRANT_READ_URI_PERMISSION
— content URI पढ़ने की permission दें (FileProvider के साथ शेयर करते समय).
Common Actions
ACTION_VIEW
— किसी resource/URI को दिखाएँ (web, contact, map)।ACTION_SEND
/ACTION_SEND_MULTIPLE
— शेयर करना।ACTION_DIAL
— Dialer खोलना (बिना call किए).ACTION_CALL
— सीधे call (permission आवश्यक).ACTION_PICK
— user को कोई item चुनने देना (Gallery/Contacts).ACTION_GET_CONTENT
— किसी भी provider से content चुनना.
ACTION_CALL
, storage/media access, camera, location जैसे actions के लिए runtime permissions अनिवार्य हो सकती हैं।Intent.createChooser(...)
से user-friendly picker दें।Best Practices
- Explicit vs Implicit: sensitive data सिर्फ explicit intents में दें।
- Null Safety:
resolveActivity()
(Java) याpackageManager.resolveActivity
(Kotlin) से check करें कि handler मौजूद है। - FileProvider: files शेयर करते समय
content://
URI औरFLAG_GRANT_READ_URI_PERMISSION
दें—rawfile://
से बचें। - PendingIntent flags: Modern Android में
FLAG_IMMUTABLE
/FLAG_MUTABLE
सही रूप में चुनें। - Large Data: बहुत बड़ा data
Extras
में न भेजें—DB/Shared storage में रखें और केवल reference pass करें।
FAQs
Q1: Explicit और Implicit intent में मुख्य अंतर?
Explicit में target component तय होता है; Implicit में system action/data के आधार पर उपयुक्त app चुनता है।
Q2: PendingIntent कब use करें?
जब किसी दूसरे component/app (जैसे Notification या AlarmManager) को भविष्य में आपका intent चलाना हो—user interaction पर भी।
Q3: Sticky Broadcasts क्यों avoid?
Security/consistency concerns के कारण deprecated—आधुनिक apps में local broadcasts/WorkManager/foreground services आदि अपनाएँ।
Q4: क्या हर implicit intent के लिए chooser दिखाना चाहिए?
अक्सर हाँ—बेहतर UX और user को control देने के लिए Intent.createChooser()
उपयोग करें।
Comments
Post a Comment