国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android打開GPS導航并獲取位置信息返回null解決方案

2020-04-11 12:35:07
字體:
來源:轉載
供稿:網友

最近在做一個 Android 項目,需要用到GPS獲取位置信息,從 API 查了一下,發現獲取位置信息僅需極其簡單的一句即可:

復制代碼 代碼如下:

getLastKnownLocation(LocationManager.GPS_PROVIDER),

于是高興地不得了。可是一寫進代碼里,返回值(Location 類型)居然一直為null..郁悶的不得了。在網上查了好久,發現好多人都和我一樣糾結于這個問題上,有人說是因為GPS沒打開,也有人說是相關權限沒加上..可是我的明明已經在設置里打開,權限自然也加上了。在api上糾結了半天,終于找出原因了,原來要打開GPS其實在于這句:
 
復制代碼 代碼如下:
 
setTestProviderEnabled("gps",true);

而跟手機上的設置沒多大關系(起碼在我的手機上測是這樣的)。手機上的設置關閉了,這一句照樣能打開;而即使手機設置打開了,沒這一句也是白搭。與這句對應的是
 
復制代碼 代碼如下:
 
setTestProviderEnabled("gps",false);

用來關閉GPS.
GPS打開后可以用上面的方法獲取Location了嗎?還是不可以!確切地說是有時候可以,因為這個函數獲取的是上次已經獲得的位置信息,設想如果此程序第一次跑,先前并沒有獲取過位置信息,當然返回值為null了。經仔細查看api,在
復制代碼 代碼如下:

requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

里發現了這樣一句話:It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method. 因此為了獲取位置信息,應該用此方法為manager設置監聽器,在監聽器中onLocationChanged(Location location)里獲取。
測試代碼如下:
復制代碼 代碼如下:

public void onLocationChanged(Location location)
            {
                Log.i("onLocationChanged", "come in");
                if (location != null)
                {
                    Log.w("Location","Current altitude = "+ location.getAltitude());
                    Log.w("Location","Current latitude = "+ location.getLatitude());
                }
            }
    

經過測試,經過一段時間后可以獲取Location(獲取時間與minTime、minDistance相關)。還需注意的一個問題是在設置了監聽器后,刪除監聽器之前不能用上面的方法關閉gps,否則會報錯。因此關閉gps的方法是
 
復制代碼 代碼如下:
 
manager.removeUpdates (listener);//listener 即為監聽器實例
manager.setTestProviderEnabled("gps",false);

以下是測試代碼,所需權限有:
復制代碼 代碼如下:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>

復制代碼 代碼如下:

import android.app.Activity;
 import android.content.Context;
 import android.location.Criteria;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.util.Log;
 public class audio extends Activity
 {
     /** Called when the activity is first created. */
     LocationManager locationManager;
     LocationListener llistener;
     String provider;
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Criteria criteria = new Criteria();
         criteria.setAccuracy(Criteria.ACCURACY_FINE);
         criteria.setAltitudeRequired(false);
         criteria.setBearingRequired(false);
         criteria.setCostAllowed(true);
         criteria.setPowerRequirement(Criteria.POWER_LOW);
         String serviceName = Context.LOCATION_SERVICE;
         locationManager = (LocationManager) getSystemService(serviceName);
         locationManager.setTestProviderEnabled("gps", true);
         provider = locationManager.getBestProvider(criteria, true);
         Log.d("provider", provider);
         llistener = new LocationListener() {
             @Override
             public void onLocationChanged(Location location)
             {
                 // TODO Auto-generated method stub
                 Log.i("onLocationChanged", "come in");
                 if (location != null)
                 {
                     Log.w("Location", "Current altitude = "
                             + location.getAltitude());
                     Log.w("Location", "Current latitude = "
                             + location.getLatitude());
                 }
                 locationManager.removeUpdates(this);
                 locationManager.setTestProviderEnabled(provider, false);
             }
             @Override
             public void onProviderDisabled(String provider)
             {
                 // TODO Auto-generated method stub
                 Log.i("onProviderDisabled", "come in");
             }
             @Override
             public void onProviderEnabled(String provider)
             {
                 // TODO Auto-generated method stub
                 Log.i("onProviderEnabled", "come in");
             }
             @Override
             public void onStatusChanged(String provider, int status,
                     Bundle extras)
             {
                 // TODO Auto-generated method stub
                 Log.i("onStatusChanged", "come in");
             }
         };
          locationManager.requestLocationUpdates(provider, 1000, (float) 1000.0, llistener);
     }
     protected void onDestroy()
     {
         locationManager.removeUpdates(llistener);
         locationManager.setTestProviderEnabled(provider, false);
         super.onDestroy();
     }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 左贡县| 合肥市| 潢川县| 舟曲县| 平武县| 内黄县| 霍林郭勒市| 太谷县| 黎平县| 东台市| 赤水市| 顺平县| 东安县| 元江| 大庆市| 怀宁县| 和林格尔县| 新昌县| 天津市| 嘉兴市| 井陉县| 彰化县| 涪陵区| 双城市| 通渭县| 乐清市| 崇义县| 荔浦县| 屏东市| 株洲市| 曲松县| 台中县| 曲麻莱县| 宜丰县| 百色市| 新巴尔虎左旗| 新乐市| 镶黄旗| 元氏县| 房产| 大邑县|