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

Android-এ Intent কী এবং কীভাবে ব্যবহার করবেন?

Intent হলো স্ক্রিনে একটি অ্যাকশন পারফর্ম করার জন্য। এটি প্রধানত এক্টিভিটি শুরু করতে, ব্রডকাস্ট রিসিভার পাঠাতে, সার্ভিস শুরু করতে এবং দুটি এক্টিভিটির মধ্যে মেসেজ পাঠাতে ব্যবহার হয়। Android-এ দুটি ধরণের Intent রয়েছে: Implicit Intents এবং Explicit Intents। এখানে পুরনো এক্টিভিটির থেকে নতুন এক্টিভিটি শুরু করার একটি উদাহরণ দেওয়া হলো।

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

File → New Project → ডিটেইল পূরণ করুন।

ধাপ ২: res/layout/activity_main.xml (প্রথম এক্টিভিটি লেয়াউট)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send to another activity"
            android:id="@+id/send"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

ধাপ ৩: res/layout/ ফোল্ডারে নতুন লেয়াউট তৈরি করুন (দ্বিতীয় এক্টিভিটি লেয়াউট)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    tools:context=".SecondActivity">
    <TextView
        android:id="@+id/data"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

ধাপ ৪: src/MainActivity.java (প্রথম এক্টিভিটি)

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button send = findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent send = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(send);
            }
        });
    }
}

startActivity() ব্যবহার করে নতুন এক্টিভিটি শুরু করা হয়। Intent অবজেক্ট তৈরি করে করেন্ট এক্টিভিটি এবং ডেস্টিনেশন এক্টিভিটি পাস করতে হয়।

Intent send = new Intent(MainActivity.this, SecondActivity.class);
startActivity(send);

ধাপ ৫: src/SecondActivity.java (দ্বিতীয় এক্টিভিটি)

package com.example.andy.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView data = findViewById(R.id.data);
        data.setText("This is second activity");
    }
}

ধাপ ৬: AndroidManifest.xml-এ দুটি এক্টিভিটি ডিক্লেয়ার করুন

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.andy.myapplication">
    <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>
        <activity android:name=".SecondActivity"></activity>
    </application>
</manifest>

অ্যাপ্লিকেশন রান করুন।

Android-এ Intent কী এবং কীভাবে ব্যবহার করবেন?

বাটনে ক্লিক করলে নতুন এক্টিভিটি শুরু হবে।

Android-এ Intent কী এবং কীভাবে ব্যবহার করবেন?

  1. অ্যান্ড্রয়েড রুট করার আগে যা জানা দরকার

  2. অ্যান্ড্রয়েড মারশম্যালো: কী-कী নতুন?

  3. অ্যান্ড্রয়েড সিস্টেম WebView ব্যাখ্যা: এর ভূমিকা, গুরুত্ব, এবং আপডেট রাখার উপায়

  4. Android-এ Showbox: মুভি স্ট্রিমিং অ্যাপ নিরাপদে ইনস্টল ও ব্যবহার করার পদ্ধতি