如何在 Android 中檢查 GPS 接收器的當前狀態?


簡介

許多 Android 應用程式會從使用者那裡獲取位置資訊,以便將特定產品或服務送達使用者家門口。這些應用程式通常使用 GPS 獲取使用者的位置資訊來提供服務。為了從 GPS 獲取位置資訊,我們應該檢查 Android 裝置中 GPS 接收器的當前狀態,即 GPS 是否開啟。本文將介紹如何在 Android 中檢查 GPS 接收器的當前狀態。

實現

我們將建立一個簡單的應用程式,其中我們將顯示一個用於標題的 TextView,然後建立一個 TextView 用於顯示 GPS 的當前狀態(開啟或關閉)。此外,我們將新增一個按鈕,用於在我們的 Android 應用程式中檢查 GPS 的當前狀態。

步驟 1:在 Android Studio 中建立一個新專案

導航到 Android Studio,如下面的螢幕截圖所示。在下面的螢幕中,點選“新建專案”以建立一個新的 Android Studio 專案。

點選“新建專案”後,您將看到下面的螢幕。

在此螢幕中,我們只需選擇“空活動”並點選“下一步”。點選“下一步”後,您將看到下面的螢幕。

在此螢幕中,我們只需指定專案名稱。然後包名將自動生成。

注意 - 確保選擇語言為 Java。

指定所有詳細資訊後,點選“完成”以建立一個新的 Android Studio 專案。

專案建立完成後,我們將看到兩個開啟的檔案,即 activity_main.xml 和 MainActivity.java 檔案。

步驟 3:使用 activity_main.xml

導航到 activity_main.xml。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>activity_main.xml 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以便詳細瞭解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/idTVGPSStatus"
      android:layout_margin="10dp"
      android:gravity="center"
      android:text="Check Current Status of GPS Reciever in Android"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:textStyle="bold" />

   <!-- on below line displaying a text to show GPS status-->
   <TextView
      android:id="@+id/idTVGPSStatus"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:gravity="center"
      android:text="GPS Status"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="15sp"
      android:textStyle="normal" />

   <!-- on below line creating a button to check the GPS status -->
   <Button
      android:id="@+id/idBtnCheckGPS"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVGPSStatus"
      android:layout_margin="10dp"
      android:text="Check GPS Status"
      android:textAllCaps="false" />

</RelativeLayout>

說明 - 在上面的程式碼中,我們建立了一個 RelativeLayout 作為根佈局。在此 RelativeLayout 內部,我們首先顯示我們的 TextView,它將顯示應用程式的標題。之後,我們建立另一個 TextView,用於顯示當前的 GPS 狀態。最後,我們建立了一個按鈕,用於檢查 GPS 的當前狀態。

步驟 4:使用 MainActivity.java 檔案

導航到 MainActivity.java。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>MainActivity.java 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以便詳細瞭解。

package com.example.androidjavaapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.internal.FlowLayout;

public class MainActivity extends AppCompatActivity {

   // on below line we are creating variable for text view.
   private TextView gpsStatusTV;
   private Button checkGPSStatusBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line initializing variable with id.
      gpsStatusTV = findViewById(R.id.idTVGPSStatus);
      checkGPSStatusBtn = findViewById(R.id.idBtnCheckGPS);

      // on below line calling method to check GPS status
      checkGPSStatus();
      
      // on below line adding click listener for our button
      checkGPSStatusBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // on below line calling method to check gps status
            checkGPSStatus();
         }
      });
   }
   private void checkGPSStatus() {
      
      // Create a LocationManager instance
      LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
      // Check if GPS is enabled
      boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      
      // If GPS is not enabled, show the user a dialog to enable it
      if (isGPSEnabled) {
      
         // Show the dialog
         gpsStatusTV.setText("GPS is Enabled..");
      } else {
         gpsStatusTV.setText("GPS is Disabled..");
      }
   }
}

說明 - 在上面的程式碼中,我們首先建立了 TextView 的變數,用於顯示 GPS 接收器狀態,以及按鈕的變數,用於檢查 GPS 狀態。然後,我們將看到一個 onCreate 方法,在其中我們正在載入要顯示給使用者的佈局。然後,我們使用在 activity_main.xml 檔案中給出的 id 初始化按鈕和 TextView 的變數。

之後,我們建立了一個名為 checkGPS status 的方法,用於檢查當前的 GPS 狀態。在此方法中,我們檢查 GPS 狀態,並相應地將 GPS 狀態設定為我們的 TextView。現在,我們在 onCreate 方法中呼叫此方法以顯示 GPS 狀態。之後,我們為按鈕新增一個點選監聽器以檢查 GPS 狀態。在此按鈕的點選監聽器中,我們再次呼叫一個方法來檢查 GPS 狀態以顯示當前的 GPS 狀態。

新增上述程式碼後,我們只需點選頂部的綠色圖示即可在移動裝置上執行我們的應用程式。

注意 - 確保已連線到您的真實裝置或模擬器。

輸出

結論

在本文中,我們瞭解瞭如何在 Android 應用程式中獲取 GPS 接收器的當前狀態。

更新於: 2023-03-30

1K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.