[android學習]android_gps定位服務簡單實現


  • 前言
    gps定位服務的學習是這段時間gps課程的學習內容,之前老師一直在將概念,今天終於是實踐課(其實就是給了一個案例,讓自己照着敲).不過在照着案列敲了兩遍之后,發現老師的案例是在是太老了,並且直接照着案例敲,也無法理解其中很多類,方法的作用.
    於是自己在網上查看了其他實現的方法,並嘗試敲案列,期間的挫折一言難盡.(網上找的案例也並不信息,使得我在給予權限,和權限檢查方面一直報錯,因為我使用的是最新的As和java11,在經過數遍從基礎理解到實例編寫的過程和不知多少遍google之后,終於完成了這次練習)

  • 總結起來:

    • 還是發現自己有不少的問題,在代碼的理解能力上經過了這段時間的學習確實有些長進,但在較復雜的語句上面,理解還是有不小的困難.

    • 其次,在沒有事先了解學習某些類之前,是真的不適合直接照案例敲和學習(沒有十分詳細注釋的案例,通常情況下都是如此),其效率實在低下,且很多時候會不知所雲.
      (個人並不提倡照着敲,敲的多了自然就懂了的學習說法,或許它只是針對於懶的人,亦或許這種說法其實只是一個勸誡我們勤奮努力,多實踐的比喻.).


  • 源代碼
    • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@drawable/gps">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:text="您的gps定位信息是:"
        android:textColor="#fff"
        android:textSize="22dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:text="經度:"
        android:textSize="22dp"
        android:textColor="#fff"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:text="維度:"
        android:textColor="#fff"
        android:textSize="22dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:text="速度:"
        android:textSize="22dp"
        android:textColor="#fff"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:text="海拔:"
        android:textSize="22dp"
        android:textColor="#fff"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:text="方位:"
        android:textSize="22dp"
        android:textColor="#fff"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:text="時間:"
        android:textSize="22dp"
        android:textColor="#fff"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/tv_7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:text="衛星數:"
        android:textSize="22dp"
        android:textColor="#fff"
        android:textStyle="bold" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="22dp"
            android:layout_marginLeft="10dp"
            android:text="打開"/>

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:textSize="22dp"
            android:text="關閉"/>
    </LinearLayout>
</LinearLayout>
  • MainActivity.java
package cn.gemuxiaoshe.gpsapplication20;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import cn.gemuxiaoshe.gpsapplication20.Tools.Timetool;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LocationManager lm;
    private TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6, tv_7;
    private Button btn1, btn2;
    private List<GpsSatellite> numSatlliteList = new ArrayList<GpsSatellite>();  // 衛星信號
    private Location location;

    private LocationListener locationListener= new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // 當gps定位信息發生改變時,更新定位
            updateShow(location);
        }
        @Override
        public void onProviderEnabled(String provider) {
            // 當gpsLocationProvider可用時,更新定位
            updateShow(null);
        }
        @Override
        public void onProviderDisabled(String s) {
        }
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.start);
        btn2 = (Button)findViewById(R.id.stop);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        tv_5 = (TextView) findViewById(R.id.tv_5);
        tv_6 = (TextView) findViewById(R.id.tv_6);
        tv_7 = (TextView) findViewById(R.id.tv_7);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }
    //點擊事件

    public void onClick(View v) {
        if (v == btn1) {
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Toast.makeText(this, "gps已關閉,請手動打開再試!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "gps定位中...", Toast.LENGTH_SHORT).show();
            }
            // new 對象設置精度信息
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(true);
            criteria.setBearingRequired(true);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);

            String provider = lm.getBestProvider(criteria, true);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            location = lm.getLastKnownLocation(provider);
            updateShow(location);
            //設置動態監聽,時間為兩秒
            lm.requestLocationUpdates(provider,2000,0,locationListener);
            // 設置動態回調函數.statusListener是回調函數
            lm.addGpsStatusListener(statusListener);  // 注冊狀態信息回調.
        }else if (v == btn2){
            finish();
           // super.onDestroy();
           // super.onStop();
           // lm.removeUpdates(locationListener);
        }

    }

    // 衛星狀態監聽器
    GpsStatus.Listener statusListener = new GpsStatus.Listener() {
        @Override
        public void onGpsStatusChanged(int i) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            GpsStatus status = lm.getGpsStatus(null);
            updateGpsStatus(i,status);
        }
    };

    // 獲取衛星數

    private void updateGpsStatus(int event, GpsStatus status){
        if (status == null){
        }else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){
            // 獲取最大的衛星數(這只是一個預設的值)
            int maxStaellites = status.getMaxSatellites();
            Iterator<GpsSatellite> it = status.getSatellites().iterator();
            numSatlliteList.clear();
            int count = 0;
            while(it.hasNext() && count <= maxStaellites){
                GpsSatellite s = it.next();
                numSatlliteList.add(s);
                count++;
            }
        }else if(event == GpsStatus.GPS_EVENT_STARTED){
            // 定位開始
        }else if (event == GpsStatus.GPS_EVENT_STOPPED){
            // 定位結束
        }
    }

    // 定義更新顯示的方法
    private void updateShow(Location location){
        if (location!=null){

            tv_1.setText("經度:"+location.getLongitude());
            tv_2.setText("維度:"+location.getLatitude());
            tv_3.setText("海拔:"+location.getAltitude());
            tv_4.setText("速度:"+location.getSpeed());
            tv_5.setText("方位:"+location.getBearing());
            tv_6.setText("時間:"+Timetool.SdateAllTime_mc());
            tv_7.setText("衛星數:"+numSatlliteList.size());
        }else
        {
            tv_1.setText("地理位置位置或正在獲取地理位置中...");
        }
    }


    private boolean isGpsAble(LocationManager lm) {
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)?true:false;
    }


    // 打開設置界面讓用戶自己設置
    private void openGps(){
        Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
        startActivityForResult(intent,0);
    }
}

需要注意到的是:

  • 我屢次報錯的原因:

“從Android 6.0(API級別23)開始,用戶在應用程序運行時向應用程序授予權限,而不是在安裝應用程序時授予權限。” 在這種情況下,“ACCESS_FINE_LOCATION”是一個“危險權限,因此,你會得到這個'java.lang.SecurityException:”gps“位置提供者需要ACCESS_FINE_LOCATION權限。” 錯誤.

  • 解決方法:
   if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

關於該問題的詳細說明請參看:
在運行時請求權限

  • 演示

2019-4-15-01.gif

  • 這里需要注意的是:
    如果你是在模擬器上測試程序時,請手動打開應用的權限設置,並給予程序獲取定位信息的權限.否則模擬器是不會有提示的,你只會獲得下面這樣的一段崩潰記錄...

2019-4-10-01.png

就記錄到這里了,關於gps定位服務的詳細學習在之后會單獨出筆記記錄,今天是就照案列敲的一次練習,並簡記一下從中學到的的一些東西.並深刻體會下這種坑爹的學習方式.


更新時間:
2019-4-10
1:36


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM