কম্পিউটার টিউটোরিয়াল

Kotlin-এ Android-এ প্রতি ১০ সেকেন্ড পরপর একটি মেথড রান করার পদ্ধতি

পূর্ব প্রস্তুতি

এই উদাহরণটি `Handler` এবং `Runnable` ব্যবহার করে একটি মেথডকে প্রতি ১০ সেকেন্ড পরপর বারবার চালানোর পদ্ধতি দেখায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: activity_main.xml লেআউট

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    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:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Running a method every Ten seconds"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="16sp"
        android:textStyle="bold" />

</RelativeLayout>

ধাপ ৩: MainActivity.kt-এ Handler লজিক

import android.os.Bundle
import android.os.Handler
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val handler = Handler()
    private var runnable: Runnable? = null
    private val delay = 10000L // 10 seconds in milliseconds

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }

    override fun onResume() {
        super.onResume()
        runnable = Runnable {
            // এই ব্লকটি প্রতি ১০ সেকেন্ডে একবার চালাবে
            Toast.makeText(this@MainActivity, "This method will run every 10 seconds", Toast.LENGTH_SHORT).show()
            // পরবর্তী রান শিডিউল করুন
            handler.postDelayed(runnable!!, delay)
        }
        // প্রথমবার দেরি করে শুরু করুন
        handler.postDelayed(runnable!!, delay)
    }

    override fun onPause() {
        super.onPause()
        // লিক এড়াতে কলব্যাক সরিয়ে দিন
        runnable?.let { handler.removeCallbacks(it) }
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.periodictask">

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

    </application>
</manifest>

পরীক্ষা করুন

অ্যাপ চালু রাখুন। প্রতি ১০ সেকেন্ড পরপর টোস্ট মেসেজ দেখাবে। অ্যাপ ব্যাকগ্রাউন্ডে যাওয়া বা বন্ধ করলে টাইমার বন্ধ হয়ে যাবে।

আধুনিক বিকল্প

Kotlin Coroutines (`delay(10000)`) বা `WorkManager` ব্যবহার করা আরও কার্যকর এবং লাইফসাইকেল-সচেতন।

  1. Android-এ নির্দিষ্ট সময় পর মেথড কল করার পদ্ধতি

  2. Kotlin ব্যবহার করে Android-এ স্ক্রলযোগ্য TextView তৈরি করা

  3. Kotlin ব্যবহার করে Android-এ JSON অবজেক্ট পার্স করা

  4. অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে GridView স্ক্রোলিং নিষ্ক্রিয় করার উপায়