ภาพรวม
ตัวอย่างนี้แสดงวิธีตรวจสอบสถานะ GPS และเปิดหน้าตั้งค่าตำแหน่งให้ผู้ใช้เปิด/ปิด GPS เอง (Android ไม่อนุญาตให้ปิด/เปิด GPS โดยตรงผ่านโค้ดเพื่อความปลอดภัย)
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: จัดทำ 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"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonGps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="เปิด/ปิด GPS" />
<TextView
android:id="@+id/textView"
android:layout_centerInParent="true"
android:layout_marginBottom="20dp"
android:layout_above="@id/buttonGps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="บริการตำแหน่ง" />
</RelativeLayout>
ขั้นตอนที่ 3: เขียนโค้ด 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 {
TextView textView;
Button buttonGps;
Context context;
LocationManager locationManager;
boolean gpsStatus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
buttonGps = findViewById(R.id.buttonGps);
context = getApplicationContext();
checkGPSStatus();
buttonGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// เปิดหน้าตั้งค่าตำแหน่ง (Location Settings)
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
}
private void checkGPSStatus() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
gpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gpsStatus) {
textView.setText("บริการตำแหน่ง: เปิดอยู่");
} else {
textView.setText("บริการตำแหน่ง: ปิดอยู่");
}
}
@Override
protected void onResume() {
super.onResume();
checkGPSStatus(); // ตรวจสอบอีกครั้งเมื่อกลับมาจาก Settings
}
}
ขั้นตอนที่ 4: AndroidManifest.xml - สิทธิ์
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
ข้อจำกัดสำคัญ
⚠️ Android ไม่อนุญาตให้แอปเปิด/ปิด GPS โดยตรง (ตั้งแต่ API 19+)
ต้องเปิด Intent ไปยัง
ต้องเปิด Intent ไปยัง
Settings.ACTION_LOCATION_SOURCE_SETTINGS ให้ผู้ใช้กดเปิด/ปิดเอง
วิธีตรวจสอบ Provider ต่างๆ
// GPS Provider (แม่นยำ ใช้ดาวเทียม) locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) // Network Provider (ประมาณการ ใช้ WiFi/Cell Tower) locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) // Passive Provider (รับจากแอปอื่น) locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)
แนวทางปัจจุบันแนะนำ: LocationRequest + SettingsClient
// ใช้ Google Play Services - ตรวจสอบและแจ้งเตือนอัตโนมัติ
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(response -> {
// GPS พร้อมใช้งาน
}).addOnFailureListener(e -> {
// GPS ปิด - แสดง Dialog ให้เปิด
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
});
การทดสอบ
รันแอป จะแสดงสถานะ GPS ปัจจุบัน กดปุ่มจะเปิดหน้าตั้งค่าตำแหน่ง