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

অ্যান্ড্রয়েডে LocalBroadcastManager ব্যবহারের পদ্ধতি

LocalBroadcastManager ব্যবহার করে অ্যাপ্লিকেশন-লোকাল ব্রডকাস্ট পাঠানো ও গ্রহণ করা

এই উদাহরণটি দেখাবে কীভাবে লোকাল ব্রডকাস্ট ম্যানেজার ব্যবহার করে সিকিউরভাবে কম্পোনেন্ট間 ডেটা পাস করা যায়।

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

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"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:id="@+id/actionEvent"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>

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

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView actionEvent;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("custom-action-local-broadcast"));
        actionEvent = findViewById(R.id.actionEvent);
        actionEvent.setText("Click");
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("custom-action-local-broadcast");
                intent.putExtra("name", "Tutorialspoint.com");
                LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intent);
            }
        });
    }

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("name");
            actionEvent.setText(message);
        }
    };
}

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

অ্যাপ চালু করুন। টেক্সটে ক্লিক করলে ব্রডকাস্ট পাঠাবে এবং রিসিভার তা গ্রহণ করে টেক্সট আপডেট করবে।

অ্যান্ড্রয়েডে LocalBroadcastManager ব্যবহারের পদ্ধতি

অ্যান্ড্রয়েডে LocalBroadcastManager ব্যবহারের পদ্ধতি

  1. অ্যান্ড্রয়েডে Handler ব্যবহারের পদ্ধতি

  2. এন্ড্রয়েডে ViewFlipper ব্যবহার করার পদ্ধতি

  3. অ্যান্ড্রয়েডে Snackbar ব্যবহার করার পদ্ধতি

  4. Android-এ NavigationView ব্যবহার করে নেভিগেশন ড্রয়ার তৈরির পদ্ধতি