[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

原文地址:https://www.cnblogs.com/gemuxiaoshe/p/10680933.html