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

অ্যান্ড্রয়েড অ্যাপে লোকেশন সার্ভিস সক্রিয় কিনা যাচাই করার পদ্ধতি

এই টিউটোরিয়ালে আমরা শিখব কীভাবে একটি অ্যান্ড্রয়েড অ্যাপ্লিকেশনে লোকেশন সার্ভিস (GPS/নেটওয়ার্ক) সক্রিয় আছে কিনা তা প্রোগ্রাম্যাটিক্যালি যাচাই করা যায়।

প্রয়োজনীয় ধাপগুলো

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

Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট ফাইল সংশোধন করুন

res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_margin="16dp"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enable Location" />
</RelativeLayout>

ধাপ ৩: মেইন এক্টিভিটি লজিক বাস্তবায়ন করুন

src/MainActivity.java ফাইলে নিচের কোড যোগ করুন:

package app.tutorialspoint.com.sample;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.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 button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkLocationEnabled();
            }
        });
    }
    
    private void checkLocationEnabled() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean gpsEnabled = false;
        boolean networkEnabled = false;
        
        try {
            gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        try {
            networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (!gpsEnabled && !networkEnabled) {
            new AlertDialog.Builder(MainActivity.this)
                .setMessage("GPS সক্রিয় করুন")
                .setPositiveButton("সেটিংস", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                })
                .setNegativeButton("বাতিল", null)
                .show();
        }
    }
}

ধাপ ৪: ম্যানিফেস্টে পারমিশন যোগ করুন

AndroidManifest.xml ফাইলে লোকেশন পারমিশনগুলো যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.com.sample">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <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>
    </application>
</manifest>

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

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

  1. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  2. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি

  3. 如何伪造您的Android位置

  4. অ্যান্ড্রয়েড ডিভাইসে GPS লোকেশন সিমুলেট করার পদ্ধতি