Android ইন্টারফেস ডেফিনিশন ল্যাঙ্গুয়েজ (AIDL) অন্যান্য IDL-এর মতোই: এটি আপনাকে প্রোগ্রামিং ইন্টারফেস নির্ধারণ করতে দেয়, যাতে ক্লায়েন্ট ও পরিষেবা, ইন্টারপ্রসেস কমিউনিকেশন (IPC) ব্যবহার করে একে অপরের সাথে যোগাযোগ করার জন্য সম্মত হয়।
Android-এ, একটি প্রসেস সাধারণত অন্য প্রসেসের মেমরি অ্যাক্সেস করতে পারে না। কথা বলার জন্য, তাদের অবজেক্টগুলিকে প্রিমিটিভে ডিকম্পোজ করতে হবে যা অপারেটিং সিস্টেম বুঝতে পারে এবং আপনার জন্য সেই সীমানা জুড়ে অবজেক্টগুলিকে মার্শাল করতে পারে। মার্শালিং করার জন্য কোড লেখা খুবই ক্লান্তিকর, তাই Android, AIDL-এর মাধ্যমে আপনার জন্য এটি ম্যানেজ করে।
মনে রাখবেন: AIDL তখনই প্রয়োজন হয় যখন আপনি বিভিন্ন অ্যাপ্লিকেশনের ক্লায়েন্টদের
IPC-এর জন্য আপনার পরিষেবা অ্যাক্সেস করতে দেন এবং আপনার পরিষেবায় মাল্টিথ্রেডিং ম্যানেজ করতে চান। বিভিন্ন অ্যাপ্লিকেশন জুড়ে আপনাকে যদি একই সাথে IPC পারফর্ম করতে না হয়, তাহলে Binder প্রয়োগ করে
আপনার ইন্টারফেস
তৈরি করুন।
আপনি যদি IPC পারফর্ম করতে চান কিন্তু মাল্টিথ্রেডিং ম্যানেজ করার প্রয়োজন না হয়,
তাহলে Messenger ব্যবহার করে আপনার ইন্টারফেস প্রয়োগ করুন।
যাই হোক, AIDL প্রয়োগ করার আগে বাউন্ড পরিষেবা সম্পর্কে
ভালভাবে বুঝে নিন।
আপনার AIDL ইন্টারফেস ডিজাইন করা শুরু করার আগে, জেনে রাখুন যে AIDL ইন্টারফেসে কল হল ডিরেক্ট ফাংশন কল। যে থ্রেডে কল করা হয়, সেই থ্রেড সম্পর্কে কোনও অনুমান করবেন না। কলটি লোকাল প্রসেসের থ্রেড থেকে নাকি রিমোট প্রসেস থেকে করা হয়েছে তার উপর নির্ভর করে কী হবে তা ঠিক করা হয়:
- লোকাল প্রসেস থেকে করা কল একই থ্রেডে এক্সিকিউট হয় যা কল করছে। এটি
আপনার প্রধান UI থ্রেড হলে, সেই থ্রেড AIDL ইন্টারফেসে এক্সিকিউট করা চালিয়ে যায়। এটি যদি
অন্য থ্রেড হয়, তাহলে সেটিই পরিষেবায় আপনার কোড এক্সিকিউট করে। তাই, শুধুমাত্র লোকাল থ্রেড পরিষেবা অ্যাক্সেস করলে, আপনি সম্পূর্ণভাবে নিয়ন্ত্রণ করতে পারবেন যে কোন থ্রেড এতে এক্সিকিউট করছে। তবে
যদি তাই হয়, তাহলে AIDL ব্যবহার করবেন না; পরিবর্তে,
Binderপ্রয়োগ করে ইন্টারফেস তৈরি করুন। - রিমোট প্রসেস থেকে আসা কলগুলি একটি থ্রেড পুল থেকে পাঠানো হয় যা প্ল্যাটফর্মটি আপনার নিজের প্রসেসের মধ্যে মেইনটেইন করে। একই সময়ে একাধিক কল আসার সাথে সাথে অজানা থ্রেড থেকে ইনকামিং কলের জন্য প্রস্তুত থাকুন। অন্যভাবে বলতে গেলে, AIDL ইন্টারফেসের প্রয়োগ অবশ্যই সম্পূর্ণরূপে থ্রেড-সেফ হতে হবে। একই রিমোট অবজেক্টে একটি থ্রেড থেকে করা কল রিসিভারের প্রান্তে ক্রম অনুযায়ী পৌঁছায়।
onewayকীওয়ার্ড রিমোট কলের আচরণ পরিবর্তন করে। এটি ব্যবহার করা হলে, রিমোট কল ব্লক হয় না। এটি ট্রানজ্যাকশন ডেটা পাঠায় এবং সাথে সাথে ফিরে আসে। ইন্টারফেসের ইমপ্লিমেন্টেশন শেষ পর্যন্ত এটিকেBinderথ্রেড পুল থেকে একটি সাধারণ রিমোট কল হিসেবে একটি নিয়মিত কল হিসেবে গ্রহণ করে।onewayলোকাল কলের সাথে ব্যবহার করা হলে, কোনও প্রভাব পড়ে না এবং কলটি এখনও সিঙ্ক্রোনাস থাকে।
AIDL ইন্টারফেস নির্ধারণ করা হচ্ছে
Java প্রোগ্রামিং ল্যাঙ্গুয়েজ সিনট্যাক্স ব্যবহার করে .aidl ফাইলে আপনার AIDL ইন্টারফেস সংজ্ঞায়িত করুন, তারপর এটি সোর্স কোডে, src/ ডিরেক্টরিতে, পরিষেবা হোস্ট করা অ্যাপ্লিকেশন এবং পরিষেবার সাথে আবদ্ধ অন্য যেকোনও অ্যাপ্লিকেশন, উভয়ের মধ্যেই সেভ করুন।
আপনি যখন .aidl ফাইল সহ প্রতিটি অ্যাপ্লিকেশন তৈরি করেন, তখন Android SDK টুল
.aidl ফাইলের উপর ভিত্তি করে একটি IBinder ইন্টারফেস তৈরি করে এবং এটি
প্রজেক্টের gen/ ডিরেক্টরিতে সেভ করে। পরিষেবাটিকে অবশ্যই যথাযথভাবে IBinder
ইন্টারফেস প্রয়োগ করতে হবে। ক্লায়েন্ট অ্যাপ্লিকেশন তখন পরিষেবার সাথে বাইন্ড করতে এবং IPC পারফর্ম করার জন্য IBinder থেকে
কল মেথড ব্যবহার করতে পারবে।
AIDL ব্যবহার করে বাউন্ডেড পরিষেবা তৈরি করতে, নিম্নলিখিত বিভাগে বর্ণনা করা এইসব ধাপ অনুসরণ করুন:
.aidlফাইল তৈরি করুনএই ফাইলটি মেথড সিগনেচার সহ প্রোগ্রামিং ইন্টারফেসকে সংজ্ঞায়িত করে।
- ইন্টারফেস প্রয়োগ করা
Android SDK টুল আপনার
.aidlফাইলের উপর ভিত্তি করে Java প্রোগ্রামিং ল্যাঙ্গুয়েজে একটি ইন্টারফেস তৈরি করে। এই ইন্টারফেসেStubনামের একটি ইনার অ্যাবস্ট্রাক্ট ক্লাস আছে যাBinder-কে এক্সটেন্ড করে এবং আপনার AIDL ইন্টারফেস থেকে পদ্ধতি প্রয়োগ করে। আপনাকে অবশ্যইStubক্লাস এক্সটেন্ড করতে হবে এবং পদ্ধতিগুলি প্রয়োগ করতে হবে। - ক্লায়েন্টদের কাছে ইন্টারফেস এক্সপোজ করা
Stubক্লাস ইমপ্লিমেন্ট করার জন্যServiceইমপ্লিমেন্ট করুন এবংonBind()ওভাররাইড করুন।
সতর্কতা: প্রথমবার রিলিজ করার পরে আপনার AIDL ইন্টারফেসে কোনও পরিবর্তন করলে,
সেটি যেন ব্যাকওয়ার্ড কম্প্যাটিবল হয় তা নিশ্চিত করতে হবে। এর ফলে, আপনার পরিষেবা ব্যবহার করে এমন অন্যান্য অ্যাপ্লিকেশন
কাজ করা বন্ধ করে দেবে না। অর্থাৎ, আপনার .aidl ফাইলটি অবশ্যই অন্য অ্যাপ্লিকেশনে কপি করতে হবে
যাতে তারা আপনার পরিষেবার ইন্টারফেস অ্যাক্সেস করতে পারে, আপনাকে অবশ্যই আসল ইন্টারফেসের জন্য
সহায়তা বজায় রাখতে হবে।
.aidl ফাইল তৈরি করুন
AIDL একটি সহজ সিনট্যাক্স ব্যবহার করে যা আপনাকে এক বা একাধিক পদ্ধতি সহ একটি ইন্টারফেস ঘোষণা করতে দেয় যা প্যারামিটার নিতে এবং মান ফেরত দিতে পারে। প্যারামিটার এবং রিটার্ন ভ্যালু যেকোনও ধরনের হতে পারে, এমনকি অন্যান্য AIDL-জেনারেটেড ইন্টারফেসও হতে পারে।
আপনাকে Java প্রোগ্রামিং ল্যাঙ্গুয়েজ ব্যবহার করে .aidl ফাইল তৈরি করতে হবে। প্রতিটি .aidl
ফাইলকে অবশ্যই একটি ইন্টারফেস সংজ্ঞায়িত করতে হবে এবং শুধুমাত্র ইন্টারফেস ঘোষণা এবং মেথড
সিগনেচার প্রয়োজন।
ডিফল্ট হিসেবে, AIDL-এ নিম্নলিখিত ধরনের ডেটা কাজ করে:
- Java প্রোগ্রামিং ল্যাঙ্গুয়েজে
shortবাদে সব প্রিমিটিভ টাইপ (যেমনint,long,char,booleanইত্যাদি) - যেকোনও ধরনের অ্যারে, যেমন
int[]বাMyParcelable[] StringCharSequenceListList-এর সব এলিমেন্টকে এই তালিকায় উল্লেখ করা ডেটার ধরনগুলির মধ্যে একটি হতে হবে অথবা আপনার ঘোষণা করা অন্যান্য AIDL-জেনারেটেড ইন্টারফেস বা পার্সেলযোগ্য ডেটার মধ্যে একটি হতে হবে।Listবিকল্প হিসেবে প্যারামিটারাইজড টাইপ ক্লাস হিসেবে ব্যবহার করা যেতে পারে, যেমনList<String>। অন্য পক্ষ যে প্রকৃত কংক্রিট ক্লাস পায় তা সবসময়ArrayListহয়, যদিওListইন্টারফেস ব্যবহার করার জন্য পদ্ধতিটি তৈরি করা হয়।MapMap-এর সব এলিমেন্টকে এই তালিকায় উল্লেখ করা ডেটার ধরনগুলির মধ্যে একটি হতে হবে অথবা আপনার ঘোষণা করা অন্যান্য AIDL-জেনারেটেড ইন্টারফেস বা পার্সেলযোগ্য ডেটার মধ্যে একটি হতে হবে। প্যারামিটারাইজড টাইপ ম্যাপ, যেমনMap<String,Integer>ফর্মের ম্যাপ কাজ করে না। অন্য পক্ষ যে প্রকৃত কনক্রিট ক্লাস পায় তা সবসময়HashMapহয়, যদিওMapইন্টারফেস ব্যবহার করার জন্য পদ্ধতিটি তৈরি করা হয়।Map-এর বিকল্প হিসেবেBundleব্যবহার করার কথা বিবেচনা করুন।
আগে তালিকাভুক্ত করা হয়নি এমন প্রতিটি অতিরিক্ত ধরনের জন্য আপনাকে অবশ্যই একটি import স্টেটমেন্ট অন্তর্ভুক্ত করতে হবে,
এমনকি সেগুলি আপনার ইন্টারফেসের মতো একই প্যাকেজে সংজ্ঞায়িত করা হলেও।
আপনার পরিষেবা ইন্টারফেস সংজ্ঞায়িত করার সময়, সচেতন থাকুন যে:
- মেথডে শূন্য বা তার বেশি প্যারামিটার থাকতে পারে এবং এটি কোনও ভ্যালু বা ভয়েড রিটার্ন করতে পারে।
- সব নন-প্রিমিটিভ প্যারামিটারের জন্য একটি ডিরেকশনাল ট্যাগ প্রয়োজন যা ডেটা কোন দিকে যাচ্ছে তা নির্দেশ করে:
in,out, অথবাinout(নিচের উদাহরণ দেখুন)।প্রিমিটিভ,
String,IBinderএবং AIDL-জেনারেটেড ইন্টারফেস ডিফল্ট হিসেবেinএবং অন্য কিছু হতে পারে না।সতর্কতা: দিকনির্দেশকে সত্যিই যা প্রয়োজন তার মধ্যে সীমাবদ্ধ করুন, কারণ মার্শালিং প্যারামিটারগুলি ব্যয়বহুল।
.aidlফাইলে অন্তর্ভুক্ত সব কোড কমেন্ট, ইমপোর্ট ও প্যাকেজ স্টেটমেন্টের আগের কমেন্ট ছাড়া, জেনারেট করাIBinderইন্টারফেসে অন্তর্ভুক্ত করা হয়।- স্ট্রিং এবং ইন্ট কনস্ট্যান্ট AIDL ইন্টারফেসে সংজ্ঞায়িত করা যেতে পারে, যেমন
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);
}
আপনার প্রোজেক্টের src/ ডিরেক্টরিতে .aidl ফাইল সেভ করুন। আপনি যখন
অ্যাপ্লিকেশন তৈরি করেন, তখন 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. } }
জাভা
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 ইন্টারফেসকে সংজ্ঞায়িত করে। পরবর্তী ধাপে, এই ইনস্ট্যান্স ক্লায়েন্টদের কাছে প্রকাশ করা হয়
যাতে তারা পরিষেবার সাথে ইন্টার্যাক্ট করতে পারে।
আপনার AIDL ইন্টারফেস প্রয়োগ করার সময় কয়েকটি নিয়ম সম্পর্কে সচেতন থাকুন:
- ইনকামিং কল যে মূল থ্রেডে এক্সিকিউট হবেই তার কোনও গ্যারান্টি নেই, তাই আপনাকে শুরু থেকেই মাল্টিথ্রেডিংয়ের কথা ভাবতে হবে এবং থ্রেড-সেফ হওয়ার জন্য আপনার পরিষেবা সঠিকভাবে তৈরি করতে হবে।
- ডিফল্ট হিসেবে, 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. } } }
জাভা
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
ইন্টারফেস তৈরি করে এবং ক্লায়েন্টকে 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 } }
জাভা
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-এ দেখুন।
IPC-এর মাধ্যমে অবজেক্ট পাস করা
Android 10 (API লেভেল ২৯ বা তার পরবর্তী ভার্সন)-এ, আপনি সরাসরি AIDL-এ
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;
}
আগের কোড স্যাম্পেলটি অটোমেটিক একটি জাভা ক্লাস তৈরি করে যার মধ্যে 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 } }
জাভা
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); }
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. } }
জাভা
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-এর মাধ্যমে সংজ্ঞায়িত রিমোট ইন্টারফেস কল করতে, আপনার কলিং ক্লাসে নিম্নলিখিত ধাপগুলি অনুসরণ করুন:
- প্রোজেক্ট
src/ডিরেক্টরিতে.aidlফাইল অন্তর্ভুক্ত করুন। IBinderইন্টারফেসের একটি ইনস্ট্যান্স ঘোষণা করুন, যা AIDL-এর উপর ভিত্তি করে তৈরি করা হয়েছে।ServiceConnectionপ্রয়োগ করুন।Context.bindService()-কে কল করুন, আপনারServiceConnectionইমপ্লিমেন্টেশন পাস করুন।onServiceConnected()-এর প্রয়োগে, আপনি একটিIBinderইনস্ট্যান্স পাবেন, যাকেserviceবলা হয়।YourInterfaceName.Stub.asInterface((IBinder)service)-কে কল করুন যাতে ফেরত আসা প্যারামিটারকেYourInterfaceটাইপে কাস্ট করা যায়।- আপনার ইন্টারফেসে আপনি যে পদ্ধতিগুলি সংজ্ঞায়িত করেছেন সেগুলি কল করুন। সবসময়
DeadObjectExceptionব্যতিক্রম ট্র্যাপ করুন, যা কানেকশন বিচ্ছিন্ন হয়ে গেলে থ্রো করা হয়। এছাড়াও,SecurityExceptionব্যতিক্রম ট্র্যাপ করুন, যা IPC মেথড কলে জড়িত দুটি প্রসেসে পরস্পরবিরোধী AIDL সংজ্ঞা থাকলে থ্রো করা হয়। - ডিসকানেক্ট করতে, আপনার ইন্টারফেসের ইনস্ট্যান্স সহ
Context.unbindService()কল করুন।
IPC পরিষেবায় কল করার সময় এইসব পয়েন্ট মনে রাখবেন:
- প্রসেস জুড়ে অবজেক্টের রেফারেন্স গণনা করা হয়।
- আপনি মেথড আর্গুমেন্ট হিসেবে বেনামী অবজেক্ট পাঠাতে পারবেন।
কোনও পরিষেবার সাথে বাইন্ডিং সম্পর্কে আরও তথ্যের জন্য, বাউন্ড পরিষেবার ওভারভিউ পড়ুন।
এখানে কিছু স্যাম্পেল কোড দেওয়া হল যা AIDL-এর মাধ্যমে তৈরি করা পরিষেবা কল করার পদ্ধতি দেখায়। এটি 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) } } } }
জাভা
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); } } } }