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

অ্যান্ড্রয়েডে GPS প্রোগ্রামেটিক্যালি সক্ষম/অক্ষম করার পদ্ধতি

GPS স্ট্যাটাস চেক এবং সেটিংস পেজ খোলা

গুরুত্বপূর্ণ: অ্যান্ড্রয়েড **সিকিউরিটি কারণে অ্যাপ থেকে সরাসরি GPS অন/অফ করা অনুমতি দেয় না**। ব্যবহারকারীকে সেটিংস পেজে নিয়ে যেতে হয়। এই উদাহরণটি GPS স্ট্যাটাস চেক করা এবং সেটিংস পেজ খোলা দেখায়।

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

ধাপ ২: লেআউট

<?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"
    tools:context="MainActivity">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:text="Click Here to Enable Disable GPS location service!" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:gravity="center"
        android:text="Location service Status Shows Here"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

ধাপ ৩: MainActivity.java

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button button;
    Context context;
    Intent intent1;
    TextView textview;
    LocationManager locationManager;
    boolean gpsStatus;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = findViewById(R.id.button1);
        textview = findViewById(R.id.textView1);
        context = getApplicationContext();
        
        checkGpsStatus();
        
        button.setOnClickListener(v -> {
            // GPS সেটিংস পেজ খুলুন
            intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent1);
        });
    }
    
    public void checkGpsStatus() {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        
        if (locationManager != null) {
            gpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            
            if (gpsStatus) {
                textview.setText("GPS Is Enabled");
            } else {
                textview.setText("GPS Is Disabled");
            }
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        checkGpsStatus(); // ফিরে আসার সময় আপডেট করুন
    }
}

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
// অথবা ACCESS_COARSE_LOCATION (কম নির্ভুল)

GPS প্রোভাইডার টাইপ

প্রোভাইডারকনস্ট্যান্টবিবরণ
GPSGPS_PROVIDERউপগ্রহ-ভিত্তিক, উচ্চ নির্ভুলতা, আউটডোরে ভালো
NetworkNETWORK_PROVIDERওয়ায়া-ফাই/মোবাইল টাওয়ার, মাঝারি নির্ভুলতা, ইন্ডোরে কাজ করে
PassivePASSIVE_PROVIDERঅন্য অ্যাপের লোকেশন ব্যবহার করে (ব্যাটারি বাঁচায়)
FusedFusedLocationProviderClient**সেরা পদ্ধতি** - Google Play Services, অটো সিলেক্ট, ব্যাটারি দক্ষ

আধুনিক পদ্ধতি: FusedLocationProviderClient

// build.gradle
implementation "com.google.android.gms:play-services-location:21.0.1"

// চেক করা
FusedLocationProviderClient fused = LocationServices.getFusedLocationProviderClient(this);
LocationRequest request = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(request);

SettingsClient client = LocationServices.getSettingsClient(this);
client.checkLocationSettings(builder.build())
    .addOnSuccessListener(response -> { /* GPS ready */ })
    .addOnFailureListener(e -> {
        if (e instanceof ResolvableApiException) {
            // ব্যবহারকারীকে GPS অন করার ডায়লগ দেখাবে
            ((ResolvableApiException) e).startResolutionForResult(this, REQUEST_CHECK_SETTINGS);
        }
    });

সতর্কতা

  • সিটেম সেটিংস পেজ খোলার পর ব্যবহারকারী GPS অন করলে আপনার অ্যাপে ফিরে আসলে onResume()-এ পুনরায় চেক করুন
  • Android 10+ এ ব্যাকগ্রাউন্ড লোকেশন পারমিশন আলাদা (ACCESS_BACKGROUND_LOCATION)
  • ইমার্জেন্সি/সিস্টেম অ্যাপ ছাড়া কেউ প্রোগ্রামেটিক্যালি GPS টগল করতে পারেনা
  1. অ্যান্ড্রয়েডে প্রোগ্রাম্যাটিক্যালি ফোন নম্বর পাওয়ার পদ্ধতি

  2. অ্যান্ড্রয়েডে ইনকামিং কল প্রোগ্রামেটিক্যালি রিসিভ করবেন কীভাবে?

  3. অ্যান্ড্রয়েডে ScrollView প্রোগ্রাম্যাটিক্যালি ডিজেবল করার পদ্ধতি

  4. Android-এ Google Feed সক্রিয় বা নিষ্ক্রিয় করা: ধাপে ধাপে গাইড