การตรวจสอบสถานะบริการตำแหน่ง (Location Services) ในแอป Android
ตัวอย่างนี้แสดงวิธีการตรวจสอบว่าผู้ใช้ได้เปิดใช้บริการตำแหน่ง (GPS/Network) บนอุปกรณ์ Android หรือยัง หากยังไม่ได้เปิด จะแสดงกล่องโต้ตอบให้ผู้ใช้ไปเปิดที่การตั้งค่า
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio → File → New Project → กรอกรายละเอียดครบถ้วน → Finish
ขั้นตอนที่ 2: จัดหน้าตา (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:layout_margin="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="เปิดใช้ตำแหน่ง" />
</RelativeLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก - 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(v -> locationEnabled());
}
private void locationEnabled() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = false;
boolean networkEnabled = false;
try {
gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
}
try {
networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
}
if (!gpsEnabled && !networkEnabled) {
new AlertDialog.Builder(this)
.setMessage("กรุณาเปิดใช้บริการตำแหน่ง")
.setPositiveButton("การตั้งค่า", (dialog, which) ->
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)))
.setNegativeButton("ยกเลิก", null)
.show();
}
}
}
ขั้นตอนที่ 4: เพิ่มสิทธิ์ใน 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>
การทดสอบ
รันแอปบนอุปกรณ์จริง กดปุ่ม "เปิดใช้ตำแหน่ง" หาก GPS และ Network ปิดอยู่ จะแสดงกล่องโต้ตอบนำทางไปที่หน้าตั้งค่าตำแหน่ง