উদাহরণের আগে জানা দরকার Intent Service কী। Intent Service ব্যাকগ্রাউন্ড অপারেশন অ্যাসিঙ্ক্রোনাসলি করে। যখন Activity থেকে startService() কল করা হয়, তখন প্রতিটি রিকোয়ারেস্টের জন্য নতুন ইনস্ট্যান্স তৈরি হয় না। সার্ভিস ক্লাসে কাজ শেষ হলে এটি স্বয়ংক্রিয়ভাবে বন্ধ হয় অথবা stopSelf() ব্যবহার করে বন্ধ করতে হয়।
এই উদাহরণটি দেখায় কীভাবে প্রতিটি ১০ সেকেন্ড পরপর Intent Service শুরু করা যায়।
প্রয়োজনীয় ধাপসমূহ
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
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>
ধাপ ৩: MainActivity.java-এ Timer এবং BroadcastReceiver বাস্তবায়ন করুন
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.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView text;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String someValue = intent.getStringExtra("someName");
text.setText(someValue);
}
};
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.andy.myapplication");
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
final Handler handler = new Handler();
TimerTask timertask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
startService(new Intent(MainActivity.this, MyService.class));
}
});
}
};
Timer timer = new Timer();
timer.schedule(timertask, 0, 10000); // ১০ সেকেন্ড ইন্টারভাল
}
@Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}
}
ধাপ ৪: Service ক্লাস তৈরি করুন (MyService.java)
package com.example.andy.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
public class MyService extends IntentService {
public static volatile boolean shouldStop = false;
public MyService() {
super(MyService.class.getSimpleName());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Intent intent1 = new Intent("com.example.andy.myapplication");
intent1.putExtra("someName", "Tutorialspoint.com");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
if (shouldStop) {
stopSelf();
return;
}
}
}
ধাপ ৫: 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>
কিভাবে কাজ করে
- Timer এবং TimerTask ব্যবহার করে প্রতি ১০ সেকেন্ডে (১০০০০ মিলিসেকেন্ড) সার্ভিস শুরু করা হয়
- Handler ব্যবহার করে UI থ্রেডে
startService()কল করা হয় - LocalBroadcastManager ব্যবহার করে সার্ভিস থেকে Activity-তে ডেটা পাঠানো হয়
shouldStopফ্ল্যাগ ব্যবহার করে সার্ভিস বন্ধ করা যায়
অ্যাপ্লিকেশন চালান
আপনার অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে প্রজেক্টের অ্যাক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং আউটপুট দেখুন।

উপরের ফলাফলে অ্যাপ্লিকেশনের ডিফল্ট স্ক্রিন দেখানো হচ্ছে। প্রতি ১০ সেকেন্ডে সার্ভিস শুরু হবে, ব্যাকগ্রাউন্ড থেকে ডেটা চেক করবে এবং UI আপডেট করবে।