Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách kiểm tra GPS có bật hay không trên thiết bị Android

Ví dụ này minh họa cách kiểm tra trạng thái GPS (bật/tắt) và hiển thị dialog dẫn người dùng đến cài đặt nếu GPS đang tắt.

Bước 1: Tạo dự án mới

Tạo dự án mới trong Android Studio.

Bước 2: Cấu hình layout (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:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:textStyle="bold"
        android:text="Đang kiểm tra GPS..." />
</RelativeLayout>

Bước 3: Code MainActivity (src/MainActivity.java)

package com.app.sample;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        
        if (Objects.requireNonNull(locationManager).isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "GPS đang BẬT trên thiết bị", Toast.LENGTH_SHORT).show();
        } else {
            showGPSDisabledAlert();
        }
    }
    
    private void showGPSDisabledAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("GPS đang TẮT. Bạn có muốn bật không?")
               .setCancelable(false)
               .setPositiveButton("Mở cài đặt GPS", (dialog, id) -> {
                   Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                   startActivity(intent);
               })
               .setNegativeButton("Hủy", (dialog, id) -> dialog.cancel());
        
        AlertDialog alert = builder.create();
        alert.show();
    }
}

Bước 4: Cấu hình Manifest (thêm quyền location)

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

Chạy ứng dụng

Chạy app. Nếu GPS tắt sẽ hiện dialog hỏi có mở cài đặt không. Chọn "Mở cài đặt GPS" sẽ chuyển đến màn hình Location settings.

Cách kiểm tra GPS có bật hay không trên thiết bị Android Cách kiểm tra GPS có bật hay không trên thiết bị Android