欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android打開(kāi)GPS導(dǎo)航并獲取位置信息返回null解決方案

 更新時(shí)間:2013年01月13日 14:24:35   作者:  
最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來(lái)與大家分享

最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可:

復(fù)制代碼 代碼如下:

getLastKnownLocation(LocationManager.GPS_PROVIDER),

于是高興地不得了??墒且粚?xiě)進(jìn)代碼里,返回值(Location 類(lèi)型)居然一直為null..郁悶的不得了。在網(wǎng)上查了好久,發(fā)現(xiàn)好多人都和我一樣糾結(jié)于這個(gè)問(wèn)題上,有人說(shuō)是因?yàn)镚PS沒(méi)打開(kāi),也有人說(shuō)是相關(guān)權(quán)限沒(méi)加上..可是我的明明已經(jīng)在設(shè)置里打開(kāi),權(quán)限自然也加上了。在api上糾結(jié)了半天,終于找出原因了,原來(lái)要打開(kāi)GPS其實(shí)在于這句:
 
復(fù)制代碼 代碼如下:
 
setTestProviderEnabled("gps",true);

而跟手機(jī)上的設(shè)置沒(méi)多大關(guān)系(起碼在我的手機(jī)上測(cè)是這樣的)。手機(jī)上的設(shè)置關(guān)閉了,這一句照樣能打開(kāi);而即使手機(jī)設(shè)置打開(kāi)了,沒(méi)這一句也是白搭。與這句對(duì)應(yīng)的是
 
復(fù)制代碼 代碼如下:
 
setTestProviderEnabled("gps",false);

用來(lái)關(guān)閉GPS.
GPS打開(kāi)后可以用上面的方法獲取Location了嗎?還是不可以!確切地說(shuō)是有時(shí)候可以,因?yàn)檫@個(gè)函數(shù)獲取的是上次已經(jīng)獲得的位置信息,設(shè)想如果此程序第一次跑,先前并沒(méi)有獲取過(guò)位置信息,當(dāng)然返回值為null了。經(jīng)仔細(xì)查看api,在
復(fù)制代碼 代碼如下:

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

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

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());
                }
            }
    

經(jīng)過(guò)測(cè)試,經(jīng)過(guò)一段時(shí)間后可以獲取Location(獲取時(shí)間與minTime、minDistance相關(guān))。還需注意的一個(gè)問(wèn)題是在設(shè)置了監(jiān)聽(tīng)器后,刪除監(jiān)聽(tīng)器之前不能用上面的方法關(guān)閉gps,否則會(huì)報(bào)錯(cuò)。因此關(guān)閉gps的方法是
 
復(fù)制代碼 代碼如下:
 
manager.removeUpdates (listener);//listener 即為監(jiān)聽(tīng)器實(shí)例
manager.setTestProviderEnabled("gps",false);

以下是測(cè)試代碼,所需權(quán)限有:
復(fù)制代碼 代碼如下:

<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>

復(fù)制代碼 代碼如下:

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();
     }

相關(guān)文章

最新評(píng)論