Android इंटरफ़ेस डेफ़िनिशन लैंग्वेज (एआईडीएल)

Android इंटरफ़ेस डेफ़िनिशन लैंग्वेज (एआईडीएल) आईडीएल: इससे प्रोग्रामिंग इंटरफ़ेस को परिभाषित किया जा सकता है, तो ग्राहक और सेवा एक-दूसरे से संपर्क करने के लिए इस बात पर सहमति देते हैं कि वे इंटरप्रोसेस कम्यूनिकेशन (आईपीसी).

Android पर, एक प्रक्रिया आमतौर पर याददाश्त खो जाता है. बात करने के लिए, उन्हें अपनी वस्तुओं को ऐसे प्रिमिटिव में तोड़ना होता है जो ऑपरेटिंग सिस्टम आपके लिए उस सीमा के पार ऑब्जेक्ट को समझ सकता है और उन्हें मार्शल कर सकता है. इसके लिए कोड क्या मार्शलिंग को लिखना काफ़ी मुश्किल होता है, इसलिए Android इसे आपके लिए AIDL के ज़रिए मैनेज कर सकता है.

ध्यान दें: एआईडीएल सिर्फ़ तब ज़रूरी होता है, जब आपने क्लाइंट को अलग-अलग ऐप्लिकेशन आईपीसी के लिए आपकी सेवा का इस्तेमाल करते हैं और आपको अपनी सेवा. अगर आपको सभी यूआरएल पर एक साथ आईपीसी परफ़ॉर्म करने की ज़रूरत नहीं है अलग-अलग ऐप्लिकेशन, तो अपना इंटरफ़ेस बनाने के लिए इन्हें लागू करके Binder. अगर आपको आईपीसी चलाना है, लेकिन मल्टीथ्रेडिंग को हैंडल करने की ज़रूरत नहीं है, Messenger का इस्तेमाल करके अपना इंटरफ़ेस लागू करें. इसके बावजूद, पक्का करें कि आपने बाउंड सेवाओं को पहले एआईडीएल लागू करना.

अपना एआईडीएल इंटरफ़ेस डिज़ाइन करने से पहले, ध्यान रखें कि एआईडीएल इंटरफ़ेस को किए जाने वाले कॉल डायरेक्ट फ़ंक्शन कॉल. जिस थ्रेड में कॉल है उसके बारे में अनुमान न लगाएं होता है. कॉल, थ्रेड में मौजूद थ्रेड पर निर्भर करता है या नहीं, यह इस बात पर निर्भर करता है कि क्या होगा लोकल प्रोसेस या रिमोट प्रोसेस:

  • लोकल प्रोसेस से किए गए कॉल, उसी थ्रेड में एक्ज़ीक्यूट होते हैं जिससे कॉल किया जा रहा है. अगर आपने यह आपका मुख्य यूज़र इंटरफ़ेस (यूआई) थ्रेड है और एआईडीएल इंटरफ़ेस में थ्रेड एक्ज़ीक्यूट होता रहता है. अगर ऐसा है दूसरा थ्रेड, वह वह थ्रेड है जो सेवा में आपके कोड को एक्ज़ीक्यूट करती है. इस तरह, अगर सिर्फ़ स्थानीय थ्रेड सेवा को ऐक्सेस कर रहे हैं, तो इससे यह पूरी तरह कंट्रोल किया जा सकता है कि इसमें कौनसे थ्रेड एक्ज़ीक्यूट किए जा रहे हैं. लेकिन अगर ऐसा है, तो एआईडीएल का इस्तेमाल बिलकुल न करें; इसके बजाय, इंटरफ़ेस लागू करके Binder.
  • रिमोट प्रोसेस से आने वाले कॉल, उस थ्रेड पूल से भेजे जाते हैं जिसे प्लैटफ़ॉर्म बनाए रखता है डेटा ट्रांसफ़र करने की ज़रूरत नहीं है. कई कॉल वाले अज्ञात थ्रेड से आने वाले कॉल के लिए तैयार रहें एक ही समय पर होने चाहिए. दूसरे शब्दों में, एआईडीएल इंटरफ़ेस को लागू करने के लिए पूरी तरह से थ्रेड-सुरक्षित है. एक ही रिमोट ऑब्जेक्ट पर, एक थ्रेड से किए गए कॉल रिसीवर की ओर से ऑर्डर में आएँ.
  • oneway कीवर्ड की मदद से, रिमोट कॉल के व्यवहार में बदलाव किया जाता है. इसका इस्तेमाल करने पर, रिमोट कॉल से ब्लॉक न करें. यह ट्रांज़ैक्शन डेटा भेज देता है और तुरंत वापस आ जाता है. इंटरफ़ेस लागू करने पर, यह सामान्य रिमोट कॉल के तौर पर Binder थ्रेड पूल से सामान्य कॉल के तौर पर मिलता है. अगर oneway का इस्तेमाल स्थानीय कॉल के लिए किया जाता है, कोई असर नहीं पड़ता और कॉल अब भी सिंक्रोनस है.

AIDL इंटरफ़ेस तय करना

Java का इस्तेमाल करके, .aidl फ़ाइल में अपना AIDL इंटरफ़ेस तय करें फिर उसे दोनों की src/ डायरेक्ट्री में, दोनों में सोर्स कोड में सेव करें सेवा को होस्ट करने वाला ऐप्लिकेशन और सेवा से जुड़े अन्य ऐप्लिकेशन.

जब .aidl फ़ाइल वाले सभी ऐप्लिकेशन बनाए जाते हैं, तो Android SDK टूल .aidl फ़ाइल के आधार पर एक IBinder इंटरफ़ेस जनरेट करें और उसे इसमें सेव करें प्रोजेक्ट की gen/ डायरेक्ट्री. सेवा को IBinder लागू करना होगा सही तरीके से इंटरफ़ेस करने में मदद मिलती है. इसके बाद क्लाइंट ऐप्लिकेशन यहां से सेवा और कॉल तरीकों से बाइंड कर सकते हैं IPC चलाने के लिए IBinder.

एआईडीएल का इस्तेमाल करके सेवाओं को सीमित तौर पर इस्तेमाल करने की सुविधा सेट अप करने के लिए, यह तरीका अपनाएं. इसके बारे में यहां बताया गया है सेक्शन पढ़ें:

  1. .aidl फ़ाइल बनाएं

    यह फ़ाइल, प्रोग्रामिंग इंटरफ़ेस के बारे में जानकारी देने के लिए, मेथड सिग्नेचर की जानकारी देती है.

  2. इंटरफ़ेस को लागू करना

    Android SDK टूल, आपके ऐप्लिकेशन की शर्तों के आधार पर Java प्रोग्रामिंग भाषा में एक इंटरफ़ेस जनरेट करते हैं .aidl फ़ाइल. इस इंटरफ़ेस में Stub नाम की एक इनर ऐब्स्ट्रैक्ट क्लास है, जो Binder और आपके एआईडीएल इंटरफ़ेस से मेथड लागू करता है. आपको Stub क्लास और तरीकों को लागू करें.

  3. क्लाइंट को इंटरफ़ेस दिखाना

    Stub को लागू करने के लिए, Service लागू करें और onBind() को ओवरराइड करें क्लास.

चेतावनी: एआईडीएल इंटरफ़ेस में कोई बदलाव करने के बाद अन्य ऐप्लिकेशन को नुकसान से बचाने के लिए, आपकी पहली रिलीज़ का पुराने सिस्टम के साथ काम करना ज़रूरी है जो आपकी सेवा का इस्तेमाल करते हैं. इसका मतलब है कि आपकी .aidl फ़ाइल को अन्य ऐप्लिकेशन में कॉपी करना ज़रूरी है ताकि वे आपकी सेवा के इंटरफ़ेस तक पहुंच सकें, आपको इंटरफ़ेस पर कॉपी करने की सुविधा मिलती है.

.aidl फ़ाइल बनाएं

एआईडीएल एक सिंपल सिंटैक्स का इस्तेमाल करता है. इसकी मदद से, एक या एक से ज़्यादा ऐसे तरीकों से इंटरफ़ेस का एलान किया जा सकता है जो पैरामीटर लेने और वैल्यू दिखाने के लिए करते हैं. पैरामीटर और रिटर्न वैल्यू किसी भी टाइप की हो सकती हैं. एआईडीएल से जनरेट किए गए इंटरफ़ेस.

आपको .aidl फ़ाइल को, Java प्रोग्रामिंग भाषा का इस्तेमाल करके बनाना होगा. हर .aidl फ़ाइल में सिर्फ़ एक इंटरफ़ेस होना चाहिए. साथ ही, इसके लिए सिर्फ़ इंटरफ़ेस की जानकारी और तरीके की ज़रूरत होगी हस्ताक्षर.

डिफ़ॉल्ट रूप से, एआईडीएल इन डेटा टाइप के साथ काम करता है:

  • Java प्रोग्रामिंग भाषा में सभी प्रिमिटिव टाइप (जैसे कि int, long, char, boolean वगैरह)
  • प्रिमिटिव टाइप की कैटगरी, जैसे कि int[]
  • 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 जनरेट हुआ इंपोर्ट और पैकेज करने से पहले की टिप्पणियों को छोड़कर इंटरफ़ेस स्टेटमेंट.
  • स्ट्रिंग और इंटेजर कॉन्सटेंट को const int VERSION = 1;, जैसे AIDL इंटरफ़ेस में तय किया जा सकता है.
  • तरीकों से जुड़े कॉल 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 टूल आपके ऐप्लिकेशन में IBinder इंटरफ़ेस फ़ाइल जनरेट करते हैं प्रोजेक्ट की gen/ डायरेक्ट्री. जनरेट की गई फ़ाइल का नाम .aidl फ़ाइल के नाम से मेल खाता है, लेकिन .java एक्सटेंशन के साथ. उदाहरण के लिए, IRemoteService.aidl का नतीजा IRemoteService.java आता है.

अगर Android Studio का इस्तेमाल किया जाता है, तो इंक्रीमेंटल बिल्ड तुरंत ही बाइंडर क्लास जनरेट कर देता है. अगर Android Studio का इस्तेमाल नहीं किया जाता है, तो अगली बार Gradle टूल, बाइंडर क्लास जनरेट करता है. अपना ऐप्लिकेशन बनाएं. gradle assembleDebug के साथ अपना प्रोजेक्ट बनाएं या gradle assembleRelease .aidl फ़ाइल लिखने के तुरंत बाद, ताकि आपका कोड जनरेट की गई क्लास से लिंक हो सके.

इंटरफ़ेस को लागू करना

ऐप्लिकेशन बनाते समय, Android SDK टूल .java इंटरफ़ेस फ़ाइल जनरेट करते हैं आपकी .aidl फ़ाइल के नाम पर रखा गया है. जनरेट किए गए इंटरफ़ेस में Stub नाम की एक सब-क्लास शामिल है यह इसके पैरंट इंटरफ़ेस, जैसे कि YourInterface.Stub को लागू करने का एक छोटा सा तरीका है. साथ ही, .aidl फ़ाइल से सभी तरीकों के बारे में बताता है.

ध्यान दें: Stub भी कुछ हेल्पर मेथड के बारे में बताता है, खास तौर पर asInterface() के बारे में. इसमें, IBinder की ज़रूरत होती है. आम तौर पर, यह तरीका क्लाइंट के onServiceConnected() कॉलबैक तरीके को पास किया जाता है और स्टब इंटरफ़ेस का इंस्टेंस दिखाता है. कास्ट करने के तरीके के बारे में ज़्यादा जानने के लिए, आईपीसी को कॉल करना तरीका.

.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) का एक इंस्टेंस है, यह किसी सेवा के लिए आईपीसी इंटरफ़ेस के बारे में बताता है. अगले चरण में, इस इंस्टेंस को ताकि वे सेवा के साथ इंटरैक्ट कर सकें.

अपने एआईडीएल इंटरफ़ेस को लागू करते समय कुछ नियमों का ध्यान रखें:

  • इस बात की कोई गारंटी नहीं है कि आने वाले कॉल (इनकमिंग) मुख्य थ्रेड पर एक्ज़ीक्यूट होंगे, इसलिए आपको यह सोचना होगा कि शुरू से ही मल्टीथ्रेडिंग के बारे में बताया गया है. साथ ही, आपकी सेवा को थ्रेड से सुरक्षित बनाने के बारे में भी बताया गया है.
  • डिफ़ॉल्ट रूप से, आईपीसी कॉल सिंक्रोनस होते हैं. अगर आपको पता हो कि वह सेवा शुरू होने में किसी अनुरोध को पूरा करने के लिए मिलीसेकंड में, उसे गतिविधि के मुख्य थ्रेड से कॉल न करें. इससे ऐप्लिकेशन हैंग हो सकता है, जिससे 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 इंस्टेंस वापस किया गया तरीका.

क्लाइंट के पास इंटरफ़ेस क्लास का ऐक्सेस भी होना चाहिए. इसलिए, अगर क्लाइंट और सेवा अलग ऐप्लिकेशन हो, तो क्लाइंट के ऐप्लिकेशन में .aidl फ़ाइल की एक कॉपी होनी चाहिए अपनी src/ डायरेक्ट्री में मौजूद है, जो android.os.Binder जनरेट करती है इससे क्लाइंट को एआईडीएल तरीकों का ऐक्सेस मिलता है.

क्लाइंट को IBinder मिलने पर onServiceConnected() कॉलबैक में, इसे कॉल करना ज़रूरी है लौटाए गए आइटम को कास्ट करने के लिए YourServiceInterface.Stub.asInterface(service) YourServiceInterface टाइप के लिए पैरामीटर:

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;
    }
};

ज़्यादा सैंपल कोड के लिए, देखें RemoteService.java क्लास ApiDemos.

आईपीसी के ऊपर ऑब्जेक्ट पास करना

Android 10 (एपीआई लेवल 29 या उसके बाद के वर्शन) में, Parcelable ऑब्जेक्ट सीधे इसमें एआईडीएल. एआईडीएल इंटरफ़ेस आर्ग्युमेंट और पार्स किए जा सकने वाले अन्य पार्सल के तौर पर काम करने वाले टाइप यहां काम करता है. इससे, मैन्युअल तरीके से मार्शलिंग कोड और कस्टम क्लास. हालांकि, इससे एक बेयर स्ट्रक्चर भी बनता है. अगर कस्टम ऐक्सेसर या कोई दूसरी सुविधा अगर ज़रूरत हो, तो 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;
}

पिछला कोड सैंपल, पूर्णांक फ़ील्ड left वाली Java क्लास अपने-आप जनरेट करता है, top, right, और bottom. सभी प्रासंगिक मार्शलिंग कोड यह है कोड अपने-आप लागू हो जाता है. साथ ही, ऑब्जेक्ट को सीधे तौर पर इस्तेमाल किया जा सकता है. इसके लिए, आपको लागू करना.

आईपीसी इंटरफ़ेस का इस्तेमाल करके भी, कस्टम क्लास को एक प्रोसेस से दूसरी प्रोसेस में भेजा जा सकता है. हालांकि, पक्का करें कि आपकी क्लास का कोड आईपीसी चैनल की दूसरी तरफ़ उपलब्ध हो और आपकी क्लास को Parcelable इंटरफ़ेस के साथ काम करना चाहिए. सपोर्ट करना Parcelable ज़रूरी है क्योंकि यह Android सिस्टम को ऐसे प्रिमिटिव में बदल देता है जिन्हें मार्शल किया जा सकता है का इस्तेमाल किया जाता है.

Parcelable के साथ काम करने वाली कस्टम क्लास बनाने के लिए, यह करें फ़ॉलो किया जा रहा है:

  1. अपनी क्लास में Parcelable इंटरफ़ेस लागू करें.
  2. writeToParcel लागू करें, जो ऑब्जेक्ट की मौजूदा स्थिति को ट्रैक करता है और उसे Parcel पर लिखता है.
  3. अपनी क्लास में CREATOR नाम का एक स्टैटिक फ़ील्ड जोड़ें, जो लागू करने वाला एक ऑब्जेक्ट है Parcelable.Creator इंटरफ़ेस.
  4. आखिर में, एक .aidl फ़ाइल बनाएं जो आपके पार्स किए जा सकने वाले क्लास के बारे में जानकारी देती हो, जैसा कि यहां दिखाया गया है Rect.aidl फ़ाइल.

    अगर कस्टम बिल्ड प्रोसेस का इस्तेमाल किया जा रहा है, तो .aidl फ़ाइल को अपनी बिल्ड. सी भाषा में मौजूद हेडर फ़ाइल की तरह, इस .aidl फ़ाइल को कंपाइल नहीं किया जाता.

एआईडीएल, इन तरीकों और फ़ील्ड का इस्तेमाल उस कोड में करता है जो मार्शल और अनमार्शल में जनरेट करने के लिए जनरेट किया जाता है आपके ऑब्जेक्ट.

उदाहरण के लिए, Rect क्लास बनाने के लिए यहां एक Rect.aidl फ़ाइल दी गई है पार्सल किया जा सकता है:

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 की चार संख्याएं पढ़ता है, लेकिन यह पक्का करना आपकी ज़िम्मेदारी है कि ये संख्याएं ऐसी सीमा में हों जिसे स्वीकार किया जा सकता है वैल्यू डालें. अपने ऐप्लिकेशन को मैलवेयर से सुरक्षित रखने के बारे में ज़्यादा जानकारी के लिए, सुरक्षा से जुड़ी सलाह देखें.

पार्स किए जा सकने वाले बंडल वाले आर्ग्युमेंट के तरीके

अगर कोई तरीका ऐसा Bundle ऑब्जेक्ट स्वीकार करता है जिसमें मौजूद होना चाहिए पार्सल करने लायक है, तो पक्का करें कि आपने Bundle का क्लासलोडर पढ़ने की कोशिश करने से पहले Bundle.setClassLoader(ClassLoader) को कॉल किया Bundle से मिला डेटा. अगर ऐसा नहीं है, तो आप ClassNotFoundException में चले जाते हैं. भले ही, आपके ऐप्लिकेशन में पार्स किया जा सकने वाला एलिमेंट सही तरीके से बताया गया हो.

उदाहरण के लिए, नीचे दी गई .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);
}
अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है जैसा कि नीचे दिए गए तरीके में दिखाया गया है, ClassLoader Rect को पढ़ने से पहले, इसे 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.
    }
};

आईपीसी तरीके को कॉल करना

एआईडीएल वाले रिमोट इंटरफ़ेस को कॉल करने के लिए, यह तरीका अपनाएं कॉलिंग क्लास:

  1. प्रोजेक्ट src/ की डायरेक्ट्री में .aidl फ़ाइल शामिल करें.
  2. IBinder इंटरफ़ेस के एक इंस्टेंस का एलान करें, जिसे एआईडीएल.
  3. ServiceConnection लागू करें.
  4. Context.bindService() पर कॉल करें, लागू करने में पास करना.ServiceConnection
  5. onServiceConnected() को लागू करने पर, आपको IBinder मिलेगा इंस्टेंस, जिसे service कहा गया. कॉल करें YourInterfaceName.Stub.asInterface((IBinder)service) से लौटाए गए पैरामीटर को YourInterface टाइप में कास्ट करें.
  6. अपने इंटरफ़ेस में बताए गए तरीकों को कॉल करें. ऑलवे ट्रैप DeadObjectException अपवाद, जो तब दिए जाते हैं, जब कनेक्शन टूट जाता है. साथ ही, ट्रैप SecurityException अपवाद तब होते हैं, जब आईपीसी तरीके से कॉल में शामिल दो प्रोसेस में अलग-अलग एआईडीएल परिभाषाएं होती हैं.
  7. डिसकनेक्ट करने के लिए, अपने इंटरफ़ेस के इंस्टेंस के साथ 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&mdash;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&mdash;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);
            }
        }
    }
}