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

অ্যান্ড্রয়েডে অ্যাপ ব্যাকগ্রাউন্ডে গিয়ে ফিরে এসা ডিটেক্ট করা

অ্যাপ্লিকেশন ফোরগ্রাউন্ড এবং ব্যাকগ্রাউন্ড স্টেট ডিটেকশন

এই উদাহরণটি দেখাবে কীভাবে অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে গেলে এবং ফোরগ্রাউন্ডে ফিরে এসলে তা ডিটেক্ট করা যায়।

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

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

ধাপ ২: লেআউট ফাইল (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp" />
</LinearLayout>

ধাপ ৩: মেইন এক্টিভিটিতে কোড (src/MainActivity.java)

package com.example.myapplication;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import java.util.List;

public class MainActivity extends FragmentActivity {
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        if (!isApplicationBroughtToBackground()) {
            textView.setText("Application is foreground");
        }
    }

    private boolean isApplicationBroughtToBackground() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(getPackageName())) {
                return true;
            }
        }
        return false;
    }
}

অ্যাপ্লিকেশন চালানোর পদ্ধতি

অ্যাপ চালু করুন। হোম বাটন চাপে অ্যাপ ব্যাকগ্রাউন্ডে পাঠান, তারপর আবার অ্যাপ খুললে টেক্সট পরিবর্তন হবে।

অ্যান্ড্রয়েডে অ্যাপ ব্যাকগ্রাউন্ডে গিয়ে ফিরে এসা ডিটেক্ট করা

  1. Android-এ ব্যাক ট্যাপ জেসচার সক্ষম এবং ব্যবহার করার পদ্ধতি

  2. অ্যাপ স্ক্রিনশট ব্লক করলে অ্যান্ড্রয়েডে স্ক্রিনশট নেওয়ার উপায়

  3. Command Prompt-এ Background এবং Foreground Color পরিবর্তন করা

  4. অ্যান্ড্রয়েড ও iOS-এ ইউটিউব অডিও ব্যাকগ্রাউন্ডে চালান: সহজ ধাপাবলি