অ্যান্ড্রয়েডে ব্যবহারকারীর নিষ্ক্রিয়তা (Inactivity) শনাক্ত করা
এই উদাহরণটি দেখায় কিভাবে Handler এবং onUserInteraction() ব্যবহার করে ৫ সেকেন্ড (অথবা যেকোনো সময়) ব্যবহারকারী কোনো ইন্টারঅ্যাকশন না করলে সেটি শনাক্ত করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ধাপ ৩: MainActivity.java
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable inactivityRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inactivityRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
"User is inactive for 5 seconds",
Toast.LENGTH_SHORT).show();
// এখানে আপনার লজিক যোগ করুন (যেমন: লগআউট, স্ক্রিন লক ইত্যাদি)
}
};
startInactivityTimer();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
// ব্যবহারকারী ইন্টারঅ্যাক্ট করলে টাইমার রিসেট করুন
resetInactivityTimer();
}
private void startInactivityTimer() {
handler.postDelayed(inactivityRunnable, 5 * 1000); // ৫ সেকেন্ড
}
private void resetInactivityTimer() {
handler.removeCallbacks(inactivityRunnable);
startInactivityTimer();
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(inactivityRunnable); // মেমরি লিক এড়াতে
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.sample">
<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>
অ্যাপটি চালান
ডিভাইস সংযুক্ত করে Run ক্লিক করুন। ৫ সেকেন্ড কোনো টাচ বা ইন্টারঅ্যাকশন না করলে টোস্ট মেসেজ দেখাবে "User is inactive for 5 seconds"।
