Android इंटरफ़ेस डेफ़िनिशन लैंग्वेज (AIDL), अन्य आईडीएल से मिलती-जुलती है: इसकी मदद से, प्रोग्रामिंग इंटरफ़ेस तय किया जा सकता है. क्लाइंट और सेवा, इंटरप्रोसेस कम्यूनिकेशन (आईपीसी) का इस्तेमाल करके एक-दूसरे के साथ कम्यूनिकेट करने के लिए, इस इंटरफ़ेस पर सहमत होते हैं.
Android पर, आम तौर पर एक प्रोसेस, दूसरी प्रोसेस की मेमोरी को ऐक्सेस नहीं कर सकती. बातचीत करने के लिए, उन्हें अपने ऑब्जेक्ट को ऐसे प्राइमिटिव में बांटना होगा जिन्हें ऑपरेटिंग सिस्टम समझ सके. साथ ही, वे आपके लिए उस सीमा के ऑब्जेक्ट को मार्शल कर सकें. मार्शल करने के लिए कोड लिखना मुश्किल होता है. इसलिए, Android आपके लिए AIDL की मदद से इसे मैनेज करता है.
ध्यान दें: AIDL सिर्फ़ तब ज़रूरी होता है, जब अलग-अलग ऐप्लिकेशन के क्लाइंट को आईपीसी के लिए आपकी सेवा को ऐक्सेस करने की अनुमति दी जाती है और आपको अपनी सेवा में मल्टीथ्रेडिंग को मैनेज करना हो. अगर आपको अलग-अलग ऐप्लिकेशन में एक साथ आईपीसी करने की ज़रूरत नहीं है, तो Binder
लागू करके अपना इंटरफ़ेस बनाएं.
अगर आपको आईपीसी करना है, लेकिन आपको मल्टीथ्रेडिंग को हैंडल करने की ज़रूरत नहीं है, तो Messenger
का इस्तेमाल करके अपना इंटरफ़ेस लागू करें.
हालांकि, AIDL लागू करने से पहले, पक्का करें कि आपने बाउंड सेवाओं को अच्छी तरह से समझ लिया हो.
अपना एआईडीएल इंटरफ़ेस डिज़ाइन करने से पहले, ध्यान रखें कि एआईडीएल इंटरफ़ेस के कॉल, डायरेक्ट फ़ंक्शन कॉल होते हैं. उस थ्रेड के बारे में कोई अनुमान न लगाएं जिसमें कॉल होता है. कॉल, लोकल प्रोसेस की थ्रेड से है या रिमोट प्रोसेस की, इस आधार पर अलग-अलग होता है:
- स्थानीय प्रोसेस से किए गए कॉल, उसी थ्रेड में लागू होते हैं जिससे कॉल किया जा रहा है. अगर यह आपका मुख्य यूज़र इंटरफ़ेस (यूआई) थ्रेड है, तो वह थ्रेड AIDL इंटरफ़ेस में चलता रहता है. अगर यह कोई दूसरी थ्रेड है, तो यह सेवा में आपका कोड लागू करती है. इसलिए, अगर सिर्फ़ स्थानीय टास्क की थ्रेड ही सेवा को ऐक्सेस कर रही हैं, तो यह पूरी तरह से कंट्रोल किया जा सकता है कि कौनसी थ्रेड उसमें चल रही हैं. हालांकि, अगर ऐसा है, तो AIDL का इस्तेमाल बिलकुल न करें. इसके बजाय,
Binder
को लागू करके इंटरफ़ेस बनाएं. - किसी रिमोट प्रोसेस से आने वाले कॉल, थ्रेड पूल से भेजे जाते हैं. प्लैटफ़ॉर्म, इस पूल को आपकी प्रोसेस में मैनेज करता है. अनजान थ्रेड से आने वाले कॉल के लिए तैयार रहें. साथ ही, एक ही समय पर एक से ज़्यादा कॉल आने की संभावना भी होती है. दूसरे शब्दों में, एआईडीएल इंटरफ़ेस को लागू करने का तरीका, पूरी तरह से थ्रेड-सेफ़ होना चाहिए. एक ही रिमोट ऑब्जेक्ट पर एक थ्रेड से किए गए कॉल, रिसीवर के पास क्रम से पहुंचते हैं.
oneway
कीवर्ड, रिमोट कॉल के काम करने के तरीके में बदलाव करता है. इसका इस्तेमाल करने पर, रिमोट कॉल ब्लॉक नहीं होता. यह ट्रांज़ैक्शन डेटा भेजता है और तुरंत वापस आ जाता है. इंटरफ़ेस लागू करने पर, इसेBinder
थ्रेड पूल से सामान्य रिमोट कॉल के तौर पर सामान्य कॉल के तौर पर मिलता है. अगरoneway
का इस्तेमाल किसी स्थानीय कॉल के साथ किया जाता है, तो इसका कोई असर नहीं पड़ता और कॉल अब भी सिंक होता रहता है.
एआईडीएल इंटरफ़ेस तय करना
Java प्रोग्रामिंग भाषा के सिंटैक्स का इस्तेमाल करके, .aidl
फ़ाइल में अपना AIDL इंटरफ़ेस तय करें. इसके बाद, इसे सेवा को होस्ट करने वाले ऐप्लिकेशन और सेवा से बंधे किसी भी अन्य ऐप्लिकेशन, दोनों के सोर्स कोड में src/
डायरेक्ट्री में सेव करें.
.aidl
फ़ाइल वाले हर ऐप्लिकेशन को बिल्ड करने पर, Android SDK टूल .aidl
फ़ाइल के आधार पर एक IBinder
इंटरफ़ेस जनरेट करते हैं और उसे प्रोजेक्ट की gen/
डायरेक्ट्री में सेव करते हैं. सेवा को IBinder
इंटरफ़ेस को सही तरीके से लागू करना होगा. इसके बाद, क्लाइंट ऐप्लिकेशन सेवा से बंध सकते हैं और आईपीसी करने के लिए, IBinder
के मेथड को कॉल कर सकते हैं.
AIDL का इस्तेमाल करके बाउंडेड सेवा बनाने के लिए, यह तरीका अपनाएं. इस बारे में यहां दिए गए सेक्शन में बताया गया है:
.aidl
फ़ाइल बनानायह फ़ाइल, प्रोग्रामिंग इंटरफ़ेस को मेथड सिग्नेचर के साथ तय करती है.
- इंटरफ़ेस लागू करना
Android SDK टूल, आपकी
.aidl
फ़ाइल के आधार पर Java प्रोग्रामिंग भाषा में इंटरफ़ेस जनरेट करते हैं. इस इंटरफ़ेस मेंStub
नाम की एक ऐब्सट्रैक्ट क्लास होती है, जोBinder
को एक्सटेंड करती है और आपके एआईडीएल इंटरफ़ेस के तरीकों को लागू करती है. आपकोStub
क्लास को एक्सटेंड करना होगा और तरीके लागू करने होंगे. - क्लाइंट को इंटरफ़ेस दिखाना
Stub
क्लास को लागू करने के लिए,Service
लागू करें औरonBind()
को बदलें.
चेतावनी: पहली रिलीज़ के बाद, अपने AIDL इंटरफ़ेस में किए गए किसी भी बदलाव को, पुराने वर्शन के साथ काम करना चाहिए. ऐसा इसलिए, ताकि आपकी सेवा का इस्तेमाल करने वाले अन्य ऐप्लिकेशन काम करना बंद न कर दें. इसका मतलब है कि आपकी .aidl
फ़ाइल को दूसरे ऐप्लिकेशन में कॉपी किया जाना चाहिए, ताकि वे आपकी सेवा के इंटरफ़ेस को ऐक्सेस कर सकें. इसलिए, आपको ओरिजनल इंटरफ़ेस के लिए सहायता जारी रखनी होगी.
.aidl फ़ाइल बनाना
AIDL, सिंटैक्स के एक आसान तरीके का इस्तेमाल करता है. इसकी मदद से, एक या एक से ज़्यादा तरीकों वाले इंटरफ़ेस का एलान किया जा सकता है. ये तरीके, पैरामीटर ले सकते हैं और वैल्यू दिखा सकते हैं. पैरामीटर और रिटर्न वैल्यू किसी भी तरह की हो सकती हैं. यहां तक कि, एआईडीएल से जनरेट किए गए अन्य इंटरफ़ेस भी हो सकते हैं.
आपको Java प्रोग्रामिंग लैंग्वेज का इस्तेमाल करके, .aidl
फ़ाइल बनानी होगी. हर .aidl
फ़ाइल में एक इंटरफ़ेस होना चाहिए. साथ ही, इसमें सिर्फ़ इंटरफ़ेस डिक्लेरेशन और तरीकों के हस्ताक्षर होने चाहिए.
डिफ़ॉल्ट रूप से, AIDL इन डेटा टाइप के साथ काम करता है:
- Java प्रोग्रामिंग लैंग्वेज में
short
को छोड़कर, सभी प्राइमटिव टाइप (जैसे,int
,long
,char
,boolean
वगैरह) - किसी भी तरह के ऐरे, जैसे कि
int[]
याMyParcelable[]
String
CharSequence
List
List
में मौजूद सभी एलिमेंट, इस सूची में मौजूद डेटा टाइप में से किसी एक के होने चाहिए. इसके अलावा, वे एआईडीएल से जनरेट किए गए किसी अन्य इंटरफ़ेस या पार्सल किए जा सकने वाले किसी ऐसे आइटम में से किसी एक के भी होने चाहिए जिसका आपने एलान किया हो.List
को वैकल्पिक तौर पर पैरामीटर वाली टाइप क्लास के तौर पर इस्तेमाल किया जा सकता है, जैसे किList<String>
. दूसरे पक्ष को मिलने वाली असल क्लास हमेशाArrayList
होती है. हालांकि,List
इंटरफ़ेस का इस्तेमाल करने के लिए, तरीका जनरेट किया जाता है.Map
Map
में मौजूद सभी एलिमेंट, इस सूची में मौजूद डेटा टाइप में से किसी एक के होने चाहिए. इसके अलावा, वे एआईडीएल से जनरेट किए गए किसी इंटरफ़ेस या पार्सल किए जा सकने वाले किसी ऐसे आइटम में से किसी एक के भी होने चाहिए जिसका आपने एलान किया हो. पैरामीटर वाले टाइप मैप, जैसे किMap<String,Integer>
फ़ॉर्म के टाइप मैप काम नहीं करते. दूसरे पक्ष को मिलने वाली असली क्लास हमेशाHashMap
होती है. हालांकि,Map
इंटरफ़ेस का इस्तेमाल करने के लिए, यह तरीका जनरेट किया जाता है.Map
के विकल्प के तौर पर,Bundle
का इस्तेमाल करें.
आपको पहले से मौजूद सूची में शामिल नहीं किए गए हर टाइप के लिए, import
स्टेटमेंट शामिल करना होगा. ऐसा तब भी करना होगा, जब वे आपके इंटरफ़ेस के उसी पैकेज में तय किए गए हों.
सेवा इंटरफ़ेस तय करते समय, इन बातों का ध्यान रखें:
- तरीके में शून्य या उससे ज़्यादा पैरामीटर हो सकते हैं. साथ ही, वे कोई वैल्यू या कोई वैल्यू नहीं दिखा सकते.
- सभी नॉन-प्रिमिटिव पैरामीटर के लिए, डायरेक्शनल टैग की ज़रूरत होती है. इससे पता चलता है कि डेटा किस तरह से जाता है:
in
,out
याinout
(नीचे दिया गया उदाहरण देखें).प्राइमिटिव,
String
,IBinder
, और एआईडीएल से जनरेट किए गए इंटरफ़ेस डिफ़ॉल्ट रूप सेin
होते हैं और इनका स्टेटस नहीं बदला जा सकता.चेतावनी: सिर्फ़ ज़रूरी निर्देश दें, क्योंकि पैरामीटर को मार्शल करने की प्रोसेस महंगी होती है.
.aidl
फ़ाइल में शामिल सभी कोड टिप्पणियां, जनरेट किए गएIBinder
इंटरफ़ेस में शामिल की जाती हैं. हालांकि, इंपोर्ट और पैकेज स्टेटमेंट से पहले की गई टिप्पणियां शामिल नहीं की जाती हैं.- स्ट्रिंग और int कंसटेंट को एआईडीएल इंटरफ़ेस में तय किया जा सकता है, जैसे कि
const int VERSION = 1;
. - मेथड कॉल,
transact()
कोड से डिस्पैच किए जाते हैं. आम तौर पर, यह इंटरफ़ेस में मौजूद मेथड इंडेक्स पर आधारित होता है. इस वजह से, वर्शन मैनेज करना मुश्किल हो जाता है. इसलिए, किसी तरीके के लिए लेन-देन कोड को मैन्युअल तरीके से असाइन किया जा सकता है:void method() = 10;
. - जिन आर्ग्युमेंट के लिए वैल्यू सबमिट करना ज़रूरी नहीं है और रिटर्न टाइप के लिए,
@nullable
का इस्तेमाल करके एनोटेट करना ज़रूरी है.
यहां .aidl
फ़ाइल का एक उदाहरण दिया गया है:
// IRemoteService.aidl package com.example.android; // Declare any non-default types here with import statements. /** Example service interface */ interface IRemoteService { /** Request the process ID of this service. */ int getPid(); /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); }
अपनी .aidl
फ़ाइल को प्रोजेक्ट की src/
डायरेक्ट्री में सेव करें. ऐप्लिकेशन बनाने पर, SDK टूल आपके प्रोजेक्ट की gen/
डायरेक्ट्री में IBinder
इंटरफ़ेस फ़ाइल जनरेट करते हैं. जनरेट की गई फ़ाइल का नाम, .aidl
फ़ाइल के नाम से मेल खाता है, लेकिन इसमें .java
एक्सटेंशन है. उदाहरण के लिए, IRemoteService.aidl
से IRemoteService.java
मिलता है.
Android Studio का इस्तेमाल करने पर, इंक्रीमेंटल बिल्ड, बाइंडर क्लास को तुरंत जनरेट करता है.
अगर Android Studio का इस्तेमाल नहीं किया जाता है, तो अगली बार ऐप्लिकेशन बनाने पर, Gradle टूल बाइंडर क्लास जनरेट करता है. .aidl
फ़ाइल लिखने के बाद, gradle assembleDebug
या gradle assembleRelease
का इस्तेमाल करके अपना प्रोजेक्ट बनाएं, ताकि आपका कोड जनरेट की गई क्लास से लिंक हो सके.
इंटरफ़ेस लागू करना
ऐप्लिकेशन बनाने पर, Android SDK टूल आपकी .aidl
फ़ाइल के नाम पर एक .java
इंटरफ़ेस फ़ाइल जनरेट करते हैं. जनरेट किए गए इंटरफ़ेस में Stub
नाम का एक सबक्लास शामिल होता है. यह YourInterface.Stub
जैसे पैरंट इंटरफ़ेस का एब्स्ट्रैक्ट वर्शन होता है. साथ ही, इसमें .aidl
फ़ाइल के सभी तरीकों का एलान किया जाता है.
ध्यान दें: Stub
में कुछ हेल्पर तरीके भी तय किए गए हैं. इनमें सबसे अहम asInterface()
है, जो IBinder
लेता है. आम तौर पर, यह क्लाइंट के onServiceConnected()
कॉलबैक तरीके में पास किया जाता है और स्टब इंटरफ़ेस का एक इंस्टेंस दिखाता है. इस कास्ट को बनाने के तरीके के बारे में ज़्यादा जानने के लिए, IPC का कोई तरीका कॉल करना सेक्शन देखें.
.aidl
से जनरेट किए गए इंटरफ़ेस को लागू करने के लिए, जनरेट किए गए Binder
इंटरफ़ेस को बढ़ाएं, जैसे कि YourInterface.Stub
. साथ ही, .aidl
फ़ाइल से इनहेरिट किए गए तरीकों को लागू करें.
यहां IRemoteService
नाम के इंटरफ़ेस को लागू करने का उदाहरण दिया गया है. इसे पिछले IRemoteService.aidl
उदाहरण में बताए गए, बिना नाम वाले इंस्टेंस का इस्तेमाल करके तय किया गया है:
Kotlin
private val binder = object : IRemoteService.Stub() { override fun getPid(): Int = Process.myPid() override fun basicTypes( anInt: Int, aLong: Long, aBoolean: Boolean, aFloat: Float, aDouble: Double, aString: String ) { // Does nothing. } }
Java
private final IRemoteService.Stub binder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing. } };
अब binder
, Stub
क्लास (Binder
) का एक इंस्टेंस है, जो सेवा के लिए IPC इंटरफ़ेस तय करता है. अगले चरण में, इस इंस्टेंस को क्लाइंट के लिए उपलब्ध कराया जाता है, ताकि वे इस सेवा के साथ इंटरैक्ट कर सकें.
एआईडीएल इंटरफ़ेस लागू करते समय, इन नियमों का ध्यान रखें:
- यह ज़रूरी नहीं है कि इनकमिंग कॉल, मुख्य थ्रेड पर ही प्रोसेस हों. इसलिए, आपको शुरू से ही मल्टीथ्रेडिंग के बारे में सोचना होगा. साथ ही, अपनी सेवा को थ्रेड-सेफ़ बनाने के लिए सही तरीके से बनाना होगा.
- डिफ़ॉल्ट रूप से, आईपीसी कॉल सिंक्रोनस होते हैं. अगर आपको पता है कि किसी अनुरोध को पूरा करने में सेवा को कुछ मिलीसेकंड से ज़्यादा समय लगता है, तो उसे गतिविधि के मुख्य थ्रेड से न बुलाएं. इससे ऐप्लिकेशन हैंग हो सकता है. इस वजह से, Android पर "ऐप्लिकेशन काम नहीं कर रहा है" डायलॉग दिख सकता है. क्लाइंट में किसी अलग थ्रेड से कॉल करें.
Parcel.writeException()
के लिए रेफ़रंस दस्तावेज़ में बताए गए अपवाद टाइप ही कॉल करने वाले को भेजे जाते हैं.
क्लाइंट को इंटरफ़ेस दिखाना
अपनी सेवा के लिए इंटरफ़ेस लागू करने के बाद, आपको इसे क्लाइंट के लिए उपलब्ध कराना होगा, ताकि वे उससे कनेक्ट कर सकें. अपनी सेवा के लिए इंटरफ़ेस दिखाने के लिए, Service
को एक्सटेंड़ करें और onBind()
को लागू करें. इससे, आपकी क्लास का एक इंस्टेंस दिखेगा, जो जनरेट किए गए Stub
को लागू करता है. इस बारे में पिछले सेक्शन में बताया गया है. यहां एक उदाहरण के तौर पर ऐसी सेवा दी गई है जो क्लाइंट को IRemoteService
उदाहरण इंटरफ़ेस दिखाती है.
Kotlin
class RemoteService : Service() { override fun onCreate() { super.onCreate() } override fun onBind(intent: Intent): IBinder { // Return the interface. return binder } private val binder = object : IRemoteService.Stub() { override fun getPid(): Int { return Process.myPid() } override fun basicTypes( anInt: Int, aLong: Long, aBoolean: Boolean, aFloat: Float, aDouble: Double, aString: String ) { // Does nothing. } } }
Java
public class RemoteService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface. return binder; } private final IRemoteService.Stub binder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing. } }; }
अब, जब कोई क्लाइंट, जैसे कि कोई गतिविधि, इस सेवा से कनेक्ट करने के लिए bindService()
को कॉल करता है, तो क्लाइंट के onServiceConnected()
कॉलबैक को सेवा के onBind()
तरीके से दिखाया गया binder
इंस्टेंस मिलता है.
क्लाइंट के पास इंटरफ़ेस क्लास का ऐक्सेस भी होना चाहिए. इसलिए, अगर क्लाइंट और सेवा अलग-अलग ऐप्लिकेशन में हैं, तो क्लाइंट के ऐप्लिकेशन में src/
डायरेक्ट्री में .aidl
फ़ाइल की कॉपी होनी चाहिए. यह android.os.Binder
इंटरफ़ेस जनरेट करती है, जिससे क्लाइंट को AIDL तरीकों का ऐक्सेस मिलता है.
जब क्लाइंट को onServiceConnected()
कॉलबैक में IBinder
मिलता है, तो उसे YourServiceInterface
टाइप में रिटर्न किए गए पैरामीटर को कास्ट करने के लिए, YourServiceInterface.Stub.asInterface(service)
को कॉल करना होगा:
Kotlin
var iRemoteService: IRemoteService? = null val mConnection = object : ServiceConnection { // Called when the connection with the service is established. override fun onServiceConnected(className: ComponentName, service: IBinder) { // Following the preceding example for an AIDL interface, // this gets an instance of the IRemoteInterface, which we can use to call on the service. iRemoteService = IRemoteService.Stub.asInterface(service) } // Called when the connection with the service disconnects unexpectedly. override fun onServiceDisconnected(className: ComponentName) { Log.e(TAG, "Service has unexpectedly disconnected") iRemoteService = null } }
Java
IRemoteService iRemoteService; private ServiceConnection mConnection = new ServiceConnection() { // Called when the connection with the service is established. public void onServiceConnected(ComponentName className, IBinder service) { // Following the preceding example for an AIDL interface, // this gets an instance of the IRemoteInterface, which we can use to call on the service. iRemoteService = IRemoteService.Stub.asInterface(service); } // Called when the connection with the service disconnects unexpectedly. public void onServiceDisconnected(ComponentName className) { Log.e(TAG, "Service has unexpectedly disconnected"); iRemoteService = null; } };
ज़्यादा सैंपल कोड के लिए,
ApiDemos में
RemoteService.java
क्लास देखें.
आईपीसी के ज़रिए ऑब्जेक्ट पास करना
Android 10 (एपीआई लेवल 29 या इसके बाद के वर्शन) में, Parcelable
ऑब्जेक्ट को सीधे तौर पर एआईडीएल में तय किया जा सकता है. यहां वे टाइप भी काम करते हैं जो AIDL इंटरफ़ेस आर्ग्युमेंट और अन्य पार्सल किए जा सकने वाले आइटम के तौर पर काम करते हैं. इससे, मार्शल करने वाला कोड और कस्टम क्लास को मैन्युअल तरीके से लिखने की ज़रूरत नहीं पड़ती. हालांकि, इससे एक बेर स्ट्रक्चर भी बनता है. अगर आपको कस्टम ऐक्सेस करने वाले या अन्य फ़ंक्शन चाहिए, तो इसके बजाय Parcelable
लागू करें.
package android.graphics; // Declare Rect so AIDL can find it and knows that it implements // the parcelable protocol. parcelable Rect { int left; int top; int right; int bottom; }
ऊपर दिया गया कोड सैंपल, अपने-आप एक Java क्लास जनरेट करता है. इसमें पूर्णांक फ़ील्ड left
,
top
, right
, और bottom
होते हैं. काम का सारा मार्शलिंग कोड अपने-आप लागू हो जाता है. साथ ही, ऑब्जेक्ट का इस्तेमाल सीधे तौर पर किया जा सकता है. इसके लिए, कोई कोड जोड़ने की ज़रूरत नहीं होती.
IPC इंटरफ़ेस की मदद से, कस्टम क्लास को एक प्रोसेस से दूसरी प्रोसेस में भी भेजा जा सकता है. हालांकि,
पक्का करें कि आपकी क्लास का कोड, IPC चैनल के दूसरी तरफ़ उपलब्ध हो और आपकी क्लास में Parcelable
इंटरफ़ेस काम करता हो. Parcelable
का इस्तेमाल करना ज़रूरी है, क्योंकि इससे Android सिस्टम, ऑब्जेक्ट को प्राइमिटिव में बांट देता है. इन प्राइमिटिव को सभी प्रोसेस में मार्शल किया जा सकता है.
Parcelable
के साथ काम करने वाली कस्टम क्लास बनाने के लिए, यह तरीका अपनाएं:
- अपनी क्लास में
Parcelable
इंटरफ़ेस लागू करें. writeToParcel
लागू करें, जो ऑब्जेक्ट की मौजूदा स्थिति को लेता है और उसेParcel
में लिखता है.- अपनी क्लास में
CREATOR
नाम का स्टैटिक फ़ील्ड जोड़ें, जोParcelable.Creator
इंटरफ़ेस को लागू करने वाला ऑब्जेक्ट है. - आखिर में, एक
.aidl
फ़ाइल बनाएं, जिसमें आपकी पार्सल की जा सकने वाली क्लास के बारे में बताया गया हो. जैसा कि यहां दी गईRect.aidl
फ़ाइल में दिखाया गया है.अगर कस्टम बिल्ड प्रोसेस का इस्तेमाल किया जा रहा है, तो अपने बिल्ड में
.aidl
फ़ाइल न जोड़ें. C भाषा की हेडर फ़ाइल की तरह ही, इस.aidl
फ़ाइल को कंपाइल नहीं किया जाता.
AIDL, जनरेट किए गए कोड में इन तरीकों और फ़ील्ड का इस्तेमाल करता है, ताकि आपके ऑब्जेक्ट को मार्शल और अनमार्शल किया जा सके.
उदाहरण के लिए, यहां एक Rect.aidl
फ़ाइल दी गई है, जिससे ऐसी Rect
क्लास बनाई जा सकती है जिसे अलग-अलग हिस्सों में बांटा जा सकता है:
package android.graphics; // Declare Rect so AIDL can find it and knows that it implements // the parcelable protocol. parcelable Rect;
यहां एक उदाहरण दिया गया है, जिसमें बताया गया है कि Rect
क्लास, Parcelable
प्रोटोकॉल को कैसे लागू करती है.
Kotlin
import android.os.Parcel import android.os.Parcelable class Rect() : Parcelable { var left: Int = 0 var top: Int = 0 var right: Int = 0 var bottom: Int = 0 companion object CREATOR : Parcelable.Creator<Rect> { override fun createFromParcel(parcel: Parcel): Rect { return Rect(parcel) } override fun newArray(size: Int): Array<Rect?> { return Array(size) { null } } } private constructor(inParcel: Parcel) : this() { readFromParcel(inParcel) } override fun writeToParcel(outParcel: Parcel, flags: Int) { outParcel.writeInt(left) outParcel.writeInt(top) outParcel.writeInt(right) outParcel.writeInt(bottom) } private fun readFromParcel(inParcel: Parcel) { left = inParcel.readInt() top = inParcel.readInt() right = inParcel.readInt() bottom = inParcel.readInt() } override fun describeContents(): Int { return 0 } }
Java
import android.os.Parcel; import android.os.Parcelable; public final class Rect implements Parcelable { public int left; public int top; public int right; public int bottom; public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() { public Rect createFromParcel(Parcel in) { return new Rect(in); } public Rect[] newArray(int size) { return new Rect[size]; } }; public Rect() { } private Rect(Parcel in) { readFromParcel(in); } public void writeToParcel(Parcel out, int flags) { out.writeInt(left); out.writeInt(top); out.writeInt(right); out.writeInt(bottom); } public void readFromParcel(Parcel in) { left = in.readInt(); top = in.readInt(); right = in.readInt(); bottom = in.readInt(); } public int describeContents() { return 0; } }
Rect
क्लास में मार्शल करना आसान है. Parcel
में लिखी जा सकने वाली अन्य वैल्यू देखने के लिए, Parcel
पर मौजूद अन्य तरीकों पर एक नज़र डालें.
चेतावनी: दूसरी प्रोसेस से डेटा पाने पर, सुरक्षा से जुड़े असर को ध्यान में रखें. इस मामले में, Rect
Parcel
से चार नंबर पढ़ता है. हालांकि, यह पक्का करना आपका काम है कि ये नंबर, कॉल करने वाले के काम के लिए तय की गई वैल्यू की सीमा में हों. अपने ऐप्लिकेशन को मैलवेयर से सुरक्षित रखने के तरीके के बारे में ज़्यादा जानने के लिए, सुरक्षा से जुड़ी सलाह देखें.
ऐसे तरीके जिनमें Parcelable वाले बंडल आर्ग्युमेंट शामिल हैं
अगर कोई तरीका ऐसाBundle
ऑब्जेक्ट स्वीकार करता है जिसमें 'पार्टिकल करने लायक' ऑब्जेक्ट होने की संभावना है, तो पक्का करें कि आपने Bundle
से पढ़ने की कोशिश करने से पहले, Bundle.setClassLoader(ClassLoader)
को कॉल करके Bundle
का क्लासलोडर सेट किया हो. ऐसा न करने पर, आपको ClassNotFoundException
का सामना करना पड़ सकता है. भले ही, आपके ऐप्लिकेशन में parcelable की सही जानकारी दी गई हो.
उदाहरण के लिए, यहां दी गई .aidl
फ़ाइल का सैंपल देखें:
// IRectInsideBundle.aidl package com.example.android; /** Example service interface */ interface IRectInsideBundle { /** Rect parcelable is stored in the bundle with key "rect". */ void saveRect(in Bundle bundle); }
Rect
को पढ़ने से पहले ClassLoader
को Bundle
में साफ़ तौर पर सेट किया गया है:
Kotlin
private val binder = object : IRectInsideBundle.Stub() { override fun saveRect(bundle: Bundle) { bundle.classLoader = classLoader val rect = bundle.getParcelable<Rect>("rect") process(rect) // Do more with the parcelable. } }
Java
private final IRectInsideBundle.Stub binder = new IRectInsideBundle.Stub() { public void saveRect(Bundle bundle){ bundle.setClassLoader(getClass().getClassLoader()); Rect rect = bundle.getParcelable("rect"); process(rect); // Do more with the parcelable. } };
IPC का कोई तरीका कॉल करना
AIDL की मदद से तय किए गए किसी रिमोट इंटरफ़ेस को कॉल करने के लिए, अपनी कॉलिंग क्लास में यह तरीका अपनाएं:
.aidl
फ़ाइल को प्रोजेक्टsrc/
डायरेक्ट्री में शामिल करें.IBinder
इंटरफ़ेस का एक इंस्टेंस बताएं, जो एआईडीएल के आधार पर जनरेट होता है.ServiceConnection
लागू करें.Context.bindService()
को कॉल करें,ServiceConnection
को लागू करने के लिए.onServiceConnected()
को लागू करने पर, आपकोIBinder
का एक इंस्टेंस मिलता है, जिसेservice
कहा जाता है. रिटर्न किए गए पैरामीटर कोYourInterface
टाइप में कास्ट करने के लिए,YourInterfaceName.Stub.asInterface((IBinder)service)
को कॉल करें.- अपने इंटरफ़ेस पर तय किए गए मेथड को कॉल करें.
DeadObjectException
एक्सप्शन को हमेशा ट्रैप करें. ये एक्सप्शन, कनेक्शन टूटने पर ट्रिगर होते हैं. साथ ही,SecurityException
एक्सप्शन को ट्रैप करें. ये एक्सप्शन तब ट्रिगर होते हैं, जब IPC मेथड कॉल में शामिल दो प्रोसेस में, AIDL की अलग-अलग डेफ़िनिशन होती हैं. - डिसकनेक्ट करने के लिए, अपने इंटरफ़ेस के इंस्टेंस के साथ
Context.unbindService()
को कॉल करें.
आईपीसी सेवा को कॉल करते समय, इन बातों का ध्यान रखें:
- ऑब्जेक्ट की गिनती, सभी प्रोसेस में की जाती है.
- आपके पास, आर्ग्युमेंट के तौर पर बिना नाम वाले ऑब्जेक्ट भेजने का विकल्प होता है.
किसी सेवा से बाइंड करने के बारे में ज़्यादा जानने के लिए, बाइंड की गई सेवाओं की खास जानकारी पढ़ें.
यहां कुछ सैंपल कोड दिए गए हैं. इनमें, ApiDemos प्रोजेक्ट में मौजूद रिमोट सेवा के सैंपल से ली गई, एआईडीएल से बनाई गई सेवा को कॉल करने का तरीका बताया गया है.
Kotlin
private const val BUMP_MSG = 1 class Binding : Activity() { /** The primary interface you call on the service. */ private var mService: IRemoteService? = null /** Another interface you use on the service. */ internal var secondaryService: ISecondary? = null private lateinit var killButton: Button private lateinit var callbackText: TextView private lateinit var handler: InternalHandler private var isBound: Boolean = false /** * Class for interacting with the main interface of the service. */ private val mConnection = object : ServiceConnection { override fun onServiceConnected(className: ComponentName, service: IBinder) { // This is called when the connection with the service is // established, giving us the service object we can use to // interact with the service. We are communicating with our // service through an IDL interface, so get a client-side // representation of that from the raw service object. mService = IRemoteService.Stub.asInterface(service) killButton.isEnabled = true callbackText.text = "Attached." // We want to monitor the service for as long as we are // connected to it. try { mService?.registerCallback(mCallback) } catch (e: RemoteException) { // In this case, the service crashes before we can // do anything with it. We can count on soon being // disconnected (and then reconnected if it can be restarted) // so there is no need to do anything here. } // As part of the sample, tell the user what happened. Toast.makeText( this@Binding, R.string.remote_service_connected, Toast.LENGTH_SHORT ).show() } override fun onServiceDisconnected(className: ComponentName) { // This is called when the connection with the service is // unexpectedly disconnected—that is, its process crashed. mService = null killButton.isEnabled = false callbackText.text = "Disconnected." // As part of the sample, tell the user what happened. Toast.makeText( this@Binding, R.string.remote_service_disconnected, Toast.LENGTH_SHORT ).show() } } /** * Class for interacting with the secondary interface of the service. */ private val secondaryConnection = object : ServiceConnection { override fun onServiceConnected(className: ComponentName, service: IBinder) { // Connecting to a secondary interface is the same as any // other interface. secondaryService = ISecondary.Stub.asInterface(service) killButton.isEnabled = true } override fun onServiceDisconnected(className: ComponentName) { secondaryService = null killButton.isEnabled = false } } private val mBindListener = View.OnClickListener { // Establish a couple connections with the service, binding // by interface names. This lets other applications be // installed that replace the remote service by implementing // the same interface. val intent = Intent(this@Binding, RemoteService::class.java) intent.action = IRemoteService::class.java.name bindService(intent, mConnection, Context.BIND_AUTO_CREATE) intent.action = ISecondary::class.java.name bindService(intent, secondaryConnection, Context.BIND_AUTO_CREATE) isBound = true callbackText.text = "Binding." } private val unbindListener = View.OnClickListener { if (isBound) { // If we have received the service, and hence registered with // it, then now is the time to unregister. try { mService?.unregisterCallback(mCallback) } catch (e: RemoteException) { // There is nothing special we need to do if the service // crashes. } // Detach our existing connection. unbindService(mConnection) unbindService(secondaryConnection) killButton.isEnabled = false isBound = false callbackText.text = "Unbinding." } } private val killListener = View.OnClickListener { // To kill the process hosting the service, we need to know its // PID. Conveniently, the service has a call that returns // that information. try { secondaryService?.pid?.also { pid -> // Note that, though this API lets us request to // kill any process based on its PID, the kernel // still imposes standard restrictions on which PIDs you // can actually kill. Typically this means only // the process running your application and any additional // processes created by that app, as shown here. Packages // sharing a common UID are also able to kill each // other's processes. Process.killProcess(pid) callbackText.text = "Killed service process." } } catch (ex: RemoteException) { // Recover gracefully from the process hosting the // server dying. // For purposes of this sample, put up a notification. Toast.makeText(this@Binding, R.string.remote_call_failed, Toast.LENGTH_SHORT).show() } } // ---------------------------------------------------------------------- // Code showing how to deal with callbacks. // ---------------------------------------------------------------------- /** * This implementation is used to receive callbacks from the remote * service. */ private val mCallback = object : IRemoteServiceCallback.Stub() { /** * This is called by the remote service regularly to tell us about * new values. Note that IPC calls are dispatched through a thread * pool running in each process, so the code executing here is * NOT running in our main thread like most other things. So, * to update the UI, we need to use a Handler to hop over there. */ override fun valueChanged(value: Int) { handler.sendMessage(handler.obtainMessage(BUMP_MSG, value, 0)) } } /** * Standard initialization of this activity. Set up the UI, then wait * for the user to interact with it before doing anything. */ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.remote_service_binding) // Watch for button taps. var button: Button = findViewById(R.id.bind) button.setOnClickListener(mBindListener) button = findViewById(R.id.unbind) button.setOnClickListener(unbindListener) killButton = findViewById(R.id.kill) killButton.setOnClickListener(killListener) killButton.isEnabled = false callbackText = findViewById(R.id.callback) callbackText.text = "Not attached." handler = InternalHandler(callbackText) } private class InternalHandler( textView: TextView, private val weakTextView: WeakReference<TextView> = WeakReference(textView) ) : Handler() { override fun handleMessage(msg: Message) { when (msg.what) { BUMP_MSG -> weakTextView.get()?.text = "Received from service: ${msg.arg1}" else -> super.handleMessage(msg) } } } }
Java
public static class Binding extends Activity { /** The primary interface we are calling on the service. */ IRemoteService mService = null; /** Another interface we use on the service. */ ISecondary secondaryService = null; Button killButton; TextView callbackText; private InternalHandler handler; private boolean isBound; /** * Standard initialization of this activity. Set up the UI, then wait * for the user to interact with it before doing anything. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.remote_service_binding); // Watch for button taps. Button button = (Button)findViewById(R.id.bind); button.setOnClickListener(mBindListener); button = (Button)findViewById(R.id.unbind); button.setOnClickListener(unbindListener); killButton = (Button)findViewById(R.id.kill); killButton.setOnClickListener(killListener); killButton.setEnabled(false); callbackText = (TextView)findViewById(R.id.callback); callbackText.setText("Not attached."); handler = new InternalHandler(callbackText); } /** * Class for interacting with the main interface of the service. */ private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { // This is called when the connection with the service is // established, giving us the service object we can use to // interact with the service. We are communicating with our // service through an IDL interface, so get a client-side // representation of that from the raw service object. mService = IRemoteService.Stub.asInterface(service); killButton.setEnabled(true); callbackText.setText("Attached."); // We want to monitor the service for as long as we are // connected to it. try { mService.registerCallback(mCallback); } catch (RemoteException e) { // In this case the service crashes before we can even // do anything with it. We can count on soon being // disconnected (and then reconnected if it can be restarted) // so there is no need to do anything here. } // As part of the sample, tell the user what happened. Toast.makeText(Binding.this, R.string.remote_service_connected, Toast.LENGTH_SHORT).show(); } public void onServiceDisconnected(ComponentName className) { // This is called when the connection with the service is // unexpectedly disconnected—that is, its process crashed. mService = null; killButton.setEnabled(false); callbackText.setText("Disconnected."); // As part of the sample, tell the user what happened. Toast.makeText(Binding.this, R.string.remote_service_disconnected, Toast.LENGTH_SHORT).show(); } }; /** * Class for interacting with the secondary interface of the service. */ private ServiceConnection secondaryConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { // Connecting to a secondary interface is the same as any // other interface. secondaryService = ISecondary.Stub.asInterface(service); killButton.setEnabled(true); } public void onServiceDisconnected(ComponentName className) { secondaryService = null; killButton.setEnabled(false); } }; private OnClickListener mBindListener = new OnClickListener() { public void onClick(View v) { // Establish a couple connections with the service, binding // by interface names. This lets other applications be // installed that replace the remote service by implementing // the same interface. Intent intent = new Intent(Binding.this, RemoteService.class); intent.setAction(IRemoteService.class.getName()); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); intent.setAction(ISecondary.class.getName()); bindService(intent, secondaryConnection, Context.BIND_AUTO_CREATE); isBound = true; callbackText.setText("Binding."); } }; private OnClickListener unbindListener = new OnClickListener() { public void onClick(View v) { if (isBound) { // If we have received the service, and hence registered with // it, then now is the time to unregister. if (mService != null) { try { mService.unregisterCallback(mCallback); } catch (RemoteException e) { // There is nothing special we need to do if the service // crashes. } } // Detach our existing connection. unbindService(mConnection); unbindService(secondaryConnection); killButton.setEnabled(false); isBound = false; callbackText.setText("Unbinding."); } } }; private OnClickListener killListener = new OnClickListener() { public void onClick(View v) { // To kill the process hosting our service, we need to know its // PID. Conveniently, our service has a call that returns // that information. if (secondaryService != null) { try { int pid = secondaryService.getPid(); // Note that, though this API lets us request to // kill any process based on its PID, the kernel // still imposes standard restrictions on which PIDs you // can actually kill. Typically this means only // the process running your application and any additional // processes created by that app as shown here. Packages // sharing a common UID are also able to kill each // other's processes. Process.killProcess(pid); callbackText.setText("Killed service process."); } catch (RemoteException ex) { // Recover gracefully from the process hosting the // server dying. // For purposes of this sample, put up a notification. Toast.makeText(Binding.this, R.string.remote_call_failed, Toast.LENGTH_SHORT).show(); } } } }; // ---------------------------------------------------------------------- // Code showing how to deal with callbacks. // ---------------------------------------------------------------------- /** * This implementation is used to receive callbacks from the remote * service. */ private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() { /** * This is called by the remote service regularly to tell us about * new values. Note that IPC calls are dispatched through a thread * pool running in each process, so the code executing here is * NOT running in our main thread like most other things. So, * to update the UI, we need to use a Handler to hop over there. */ public void valueChanged(int value) { handler.sendMessage(handler.obtainMessage(BUMP_MSG, value, 0)); } }; private static final int BUMP_MSG = 1; private static class InternalHandler extends Handler { private final WeakReference<TextView> weakTextView; InternalHandler(TextView textView) { weakTextView = new WeakReference<>(textView); } @Override public void handleMessage(Message msg) { switch (msg.what) { case BUMP_MSG: TextView textView = weakTextView.get(); if (textView != null) { textView.setText("Received from service: " + msg.arg1); } break; default: super.handleMessage(msg); } } } }