[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" 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); //設定間隔兩秒獲得一次gps定位資訊 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() { @Override public void onLocationChanged(Location location) { // 當gps定位資訊發生改變時,更新定位 updateShow(location); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String provider) { // 當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("當前gps位置定位資訊:\n"); sb.append("經度:"+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; } // 開啟設定介面讓使用者自己設定 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; }
- 關於該問題的詳細說明請參看:
- 演示
-
這裡需要注意的是:
如果你是在模擬器上測試程式時,請手動開啟應用的許可權設定,並給予程式獲取定位資訊的許可權.否則模擬器是不會有提示的,你只會獲得下面這樣的一段崩潰記錄...
就記錄到這裡了,關於gps定位服務的詳細學習在之後會單獨出筆記記錄,今天是就照案列敲的一次練習,並簡記一下從中學到的的一些東西.並深刻體會下這種坑爹的學習方式.
- 參考:
更新時間:
2019-4-10
1:36