Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實(shí)現(xiàn)
前言
gps定位服務(wù)的學(xué)習(xí)是這段時間gps課程的學(xué)習(xí)內(nèi)容,之前老師一直在將概念,今天終于是實(shí)踐課(其實(shí)就是給了一個案例,讓自己照著敲).不過在照著案列敲了兩遍之后,發(fā)現(xiàn)老師的案例是在是太老了,并且直接照著案例敲,也無法理解其中很多類,方法的作用.
于是自己在網(wǎng)上查看了其他實(shí)現(xiàn)的方法,并嘗試敲案列,期間的挫折一言難盡.
(網(wǎng)上找的案例也并不信息,使得我在給予權(quán)限,和權(quán)限檢查方面一直報錯,因為我使用的是最新的As和java11,在經(jīng)過數(shù)遍從基礎(chǔ)理解到實(shí)例編寫的過程和不知多少遍google之后,終于完成了這次練習(xí))
•總結(jié)起來:
◦還是發(fā)現(xiàn)自己有不少的問題,在代碼的理解能力上經(jīng)過了這段時間的學(xué)習(xí)確實(shí)有些長進(jìn),但在較復(fù)雜的語句上面,理解還是有不小的困難.
◦其次,在沒有事先了解學(xué)習(xí)某些類之前,是真的不適合直接照案例敲和學(xué)習(xí)(沒有十分詳細(xì)注釋的案例,通常情況下都是如此),其效率實(shí)在低下,且很多時候會不知所云.
(個人并不提倡照著敲,敲的多了自然就懂了的學(xué)習(xí)說法,或許它只是針對于懶的人,亦或許這種說法其實(shí)只是一個勸誡我們勤奮努力,多實(shí)踐的比喻.).
--------------------------------------------------------------------------------
•源代碼 ◦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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:textSize="22dp"
android:textStyle="bold" />
</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.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.widget.TextView;
import android.widget.Toast;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private LocationManager lm;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show = (TextView) findViewById(R.id.tv_show);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!isGpsAble(lm)) {
Toast.makeText(MainActivity.this, "請打開Gps!", Toast.LENGTH_SHORT).show();
openGps();
}
// 從gps獲取最近的定位信息
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 lc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateShow(lc);
//設(shè)置間隔兩秒獲得一次gps定位信息
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// 當(dāng)gps定位信息發(fā)生改變時,更新定位
updateShow(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String provider) {
// 當(dāng)gpsLocationProvider可用時,更新定位
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
updateShow(lm.getLastKnownLocation(provider));
}
@Override
public void onProviderDisabled(String s) {
updateShow(null);
}
});
}
// 定義更新顯示的方法
private void updateShow(Location location){
if (location!=null){
StringBuilder sb =new StringBuilder();
sb.append("當(dāng)前gps位置定位信息:\n");
sb.append("經(jīng)度:"+location.getLongitude()+"\n");
sb.append("維度:"+location.getLatitude()+"\n");
sb.append("海拔:"+location.getAltitude()+"\n");
sb.append("速度:"+location.getSpeed()+"\n");
sb.append("方位:"+location.getBearing()+"\n");
sb.append("時間:"+location.getTime()+"\n");
sb.append("定位精度:"+location.getLongitude()+"\n");
tv_show.setText(sb.toString());
}else
tv_show.setText("");
}
private boolean isGpsAble(LocationManager lm) {
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)?true:false;
}
// 打開設(shè)置界面讓用戶自己設(shè)置
private void openGps(){
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivityForResult(intent,0);
}
}
需要注意到的是:
•我屢次報錯的原因:
“從Android 6.0(API級別23)開始,用戶在應(yīng)用程序運(yùn)行時向應(yīng)用程序授予權(quán)限,而不是在安裝應(yīng)用程序時授予權(quán)限?!?在這種情況下,“ACCESS_FINE_LOCATION”是一個“危險權(quán)限,因此,你會得到這個'java.lang.SecurityException:”gps“位置提供者需要ACCESS_FINE_LOCATION權(quán)限?!?錯誤.
•解決方法:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
•關(guān)于該問題的詳細(xì)說明請參看:
在運(yùn)行時請求權(quán)限
•演示

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

就記錄到這里了,關(guān)于gps定位服務(wù)的詳細(xì)學(xué)習(xí)在之后會單獨(dú)出筆記記錄,今天是就照案列敲的一次練習(xí),并簡記一下從中學(xué)到的的一些東西.并深刻體會下這種坑爹的學(xué)習(xí)方式.
總結(jié)
以上所述是小編給大家介紹的Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實(shí)現(xiàn),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android Studio使用Profiler來完成內(nèi)存泄漏的定位
- 解決Android原生定位的坑
- Android實(shí)現(xiàn)點(diǎn)擊某個按鈕指定位置彈出布局
- Android RecycleView滑動停止后自動吸附效果的實(shí)現(xiàn)代碼(滑動定位)
- Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼
- android studio 使用Mocklocation虛擬定位
- 解決Android 10/Android Q手機(jī)在后臺無法正常定位問題
- Android實(shí)現(xiàn)高德地圖顯示及定位
- Android使用網(wǎng)絡(luò)獲取定位的方法
- Android實(shí)現(xiàn)手機(jī)定位的案例代碼
- Android百度地圖定位、顯示用戶當(dāng)前位置
- Android中WebView控件支持地理位置定位方法
- Android 簡單服務(wù)定位器模式實(shí)現(xiàn)
相關(guān)文章
Android ExpandableListView使用方法案例詳解
這篇文章主要介紹了Android ExpandableListView使用方法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Flutter 實(shí)現(xiàn)酷炫的3D效果示例代碼
這篇文章主要介紹了Flutter 實(shí)現(xiàn)酷炫的3D效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Android實(shí)現(xiàn)自定義手勢和識別手勢的功能
這篇文章主要介紹了Android實(shí)現(xiàn)自定義手勢和識別手勢的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Android中TabLayout添加小紅點(diǎn)的示例代碼
本篇文章主要介紹了Android中TabLayout添加小紅點(diǎn)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Android高仿抖音照片電影功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android高仿抖音照片電影功能的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
Android雙重SurfaceView實(shí)現(xiàn)彈幕效果
這篇文章主要為大家詳細(xì)介紹了Android雙重SurfaceView實(shí)現(xiàn)彈幕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

