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

অ্যান্ড্রয়েড অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা যাচাই করার পদ্ধতি


অনেক পরিস্থিতিতে, আমাদের অ্যান্ড্রয়েড অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা তা জানার প্রয়োজন হয়। এই উদাহরণটি একটি অ্যান্ড্রয়েড অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা যাচাই করার পদ্ধতি প্রদর্শন করে।

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

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:id = "@+id/parent"
    xmlns:tools = "https://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".MainActivity"
    android:gravity = "center"
    android:background = "#33FFFF00"
    android:orientation = "vertical">
    <TextView
        android:id = "@+id/text"
        android:textSize = "18sp"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content" />
</LinearLayout>

উপরোক্ত লেআউটে, এতে TextView রয়েছে, যখন অ্যাপ্লিকেশন ফোরগ্রাউন্ড মোডে থাকে, তবে এটি TextView দেখাবে অথবা আপনি লগে তথ্য পাবেন।

ধাপ ৩: src/MainActivity.java-এ কোড যোগ করুন

package com.example.andy.myapplication;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    int view = R.layout.activity_main;
    TextView textView;
    Boolean isInBackground;
    
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(view);
        final LinearLayout parent = findViewById(R.id.parent);
        textView = findViewById(R.id.text);
        ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
        ActivityManager.getMyMemoryState(myProcess);
        isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        if (isInBackground) {
            Log.d("Tutorialspoint.com", "Your application is in background state");
        } else {
            textView.setText("Application is in foreground state");
        }
    }
}

উপরোক্ত কোডে, আমরা ActivityManager.RunningAppProcessInfo ব্যবহার করেছি, এতে অ্যাপ্লিকেশন প্রসেস তথ্য রয়েছে। সেই তথ্য থেকে আমাদের স্টেট খুঁজে বের করতে হবে:

ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
if (isInBackground) {
    Log.d("Tutorialspoint.com", "Your application is in background state");
} else {
    textView.setText("Application is in foreground state");
}

আপনার অ্যাপ্লিকেশন চালানোর চেষ্টা করুন। আমি ধরে নিচ্ছি আপনার আসল Android মোবাইল ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে। Android Studio থেকে অ্যাপ চালাতে, আপনার প্রজেক্টের একটি এক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং তারপর আপনার মোবাইল ডিভাইস চেক করুন যা আপনার ডিফল্ট স্ক্রিন প্রদর্শন করবে।

অ্যান্ড্রয়েড অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা যাচাই করার পদ্ধতি

উপরোক্ত ফলাফলে, অ্যাপ্লিকেশন ফোরগ্রাউন্ড স্টেটে আছে। এখন অ্যাপ্লিকেশন মিনিমাইজ করুন এবং লগক্যাট চেক করুন:

অ্যান্ড্রয়েড অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা যাচাই করার পদ্ধতি

এখন এটি দেখাচ্ছে অ্যাপ্লিকেশন ব্যাকগ্রাউন্ড স্টেটে আছে।


  1. Android অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা তা যাচাই করার পদ্ধতি

  2. Android-এ প্রোগ্রামেটিক্যালি অ্যাপ্লিকেশন বন্ধ করা

  3. অ্যান্ড্রয়েড অ্যাপ ব্যাকগ্রাউন্ডে চলতে বন্ধ করার পদ্ধতি

  4. আপনার ফোনের পারফরম্যান্স বাড়ান: সহজে ব্যাকগ্রাউন্ড অ্যান্ড্রয়েড অ্যাপস বন্ধ করুন