ইন্টেন্ট ব্যবহার করে গুগল সার্চ ইন্টিগ্রেশন
এই টিউটোরিয়ালটি দেখাবে কীভাবে.ACTION_WEB_SEARCH ইন্টেন্ট ব্যবহার করে আপনার অ্যাপ থেকে সরাসরি গুগল সার্চ চালু করা যায়।
পদক্ষেপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
পদক্ষেপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Search query..." />
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
পদক্ষেপ ৩: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button btnSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnSearch = findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(v -> {
String query = editText.getText().toString().trim();
if (!query.isEmpty()) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, query);
// ভয়েস সার্চের জন্য বিকল্প
// intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
// ফলব্যাক: ব্রাউজার খুলুন
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.com/search?q=" + Uri.encode(query)));
startActivity(browserIntent);
}
}
});
}
}
পদক্ষেপ ৪: AndroidManifest.xml - ইন্টারনেট পারমিশন
<uses-permission android:name="android.permission.INTERNET" />
গুরুত্বপূর্ণ পয়েন্টসমূহ
- ACTION_WEB_SEARCH: সিস্টেমের ডিফল্ট সার্চ অ্যাপ (সাধারণত গুগল) খোলে
- SearchManager.QUERY: সার্চ কোয়েরি পাস করার স্ট্যান্ডার্ড কী
- resolveActivity: সার্চ অ্যাপ ইনস্টল না থাকলে ক্র্যাশ প্রতিরোধ
- ফলব্যাক: ব্রাউজার ইন্টেন্ট ব্যবহার করুন বিকল্প হিসেবে
উন্নত সার্চ ফিচার
// নির্দিষ্ট সাইটে সার্চ intent.putExtra(SearchManager.QUERY, "site:github.com android tutorial"); // ভয়েস সার্চ লঞ্চ Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); startActivityForResult(voiceIntent, REQUEST_CODE);
সেরা অনুশীলন
- ইনপুট যাচাই করুন (খালি কোয়েরি প্রতিরোধ)
- সার্চ ইতিহাস সংরক্ষণ করুন (SharedPreferences/Room)
- সাজেশন/অটোকমপ্লিট যোগ করুন