উদাহরণের আগে জানা দরকার Intent Service কী। Intent Service ব্যাকগ্রাউন্ড অপারেশন অ্যাসিঙ্ক্রোনাসলি করে। যখন Activity থেকে startService() কল করা হয়, তখন প্রতিটি রিকোয়ারেস্টের জন্য নতুন ইনস্ট্যান্স তৈরি হয় না। সার্ভিস ক্লাসে কাজ শেষ হলে এটি স্বয়ংক্রিয়ভাবে বন্ধ হয় অথবা stopSelf() ব্যবহার করে বন্ধ করতে হয়।
এই উদাহরণটি দেখায় কীভাবে Intent Service থেকে UI আপডেট করা যায়।
প্রয়োজনীয় ধাপসমূহ
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় তথ্য পূরণ করুন।
ধাপ ২: লেআউট ফাইলে TextView যোগ করুন (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
উপরের কোডে একটি TextView নেওয়া হয়েছে। Intent Service থেকে ডেটা পেলে এটি আপডেট হবে।
ধাপ ৩: MainActivity.java-এ BroadcastReceiver এবং Service শুরু করার লজিক বাস্তবায়ন করুন
package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String s1 = intent.getStringExtra("DATAPASSED");
text.setText(s1);
}
};
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.andy.myapplication");
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, MyService.class));
}
});
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(broadcastReceiver);
}
}
ধাপ ৪: Service ক্লাস তৈরি করুন (MyService.java)
package com.example.andy.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends IntentService {
public MyService() {
super(MyService.class.getSimpleName());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Intent intent1 = new Intent();
intent1.setAction("com.example.andy.myapplication");
intent1.putExtra("DATAPASSED", "Tutorialspoint.com");
sendBroadcast(intent1);
}
}
ধাপ ৫: AndroidManifest.xml-এ সার্ভিস ডিক্লেয়ার করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
কিভাবে কাজ করে
- Activity-তে Dynamic BroadcastReceiver রেজিস্টার করা হয়
- TextView-এ ক্লিক করলে
startService()কল করে Intent Service শুরু করা হয় - Service
onHandleIntent()-এ ব্যাকগ্রাউন্ডে কাজ করে - কাজ শেষে Broadcast পাঠায়
- Activity-র BroadcastReceiver ব্রডকাস্ট গ্রহণ করে UI আপডেট করে
onStop()-এ Receiver আনরেজিস্টার করা হয়
অ্যাপ্লিকেশন চালান
আপনার অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে প্রজেক্টের অ্যাক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং আউটপুট দেখুন।

উপরের ফলাফলে অ্যাপ্লিকেশনের ডিফল্ট স্ক্রিন দেখানো হচ্ছে। "Start Service"-এ ক্লিক করলে সার্ভিস শুরু হবে এবং TextView আপডেট হবে।