作者meya (落寞之心)
看板Android
標題[開發] GPS cold start,啟動gps更好的寫法
時間Mon Jul 26 22:20:32 2010
當手機剛買回來或是回復成工廠值時,使用
Criteria mCriteria01 = new Criteria();
mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria01.setAltitudeRequired(false);
mCriteria01.setBearingRequired(false);
mCriteria01.setCostAllowed(true);
mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
mLocationPrivider = mLocationManager
.getBestProvider(mCriteria01, true);
mLocation = mLocationManager
.getLastKnownLocation(mLocationPrivider);
這種寫法,可能會造成應用程式force close,原因是mLocation內沒值
這時候必須要用其他的應用程式(如google map)先取得值,之後再開 所開發的程式
才沒問題
所以我改了一下寫法,加入判斷式,這樣就不會有force close的問題了,而且也不用其
他程式開gps取值
範例如下(大家可以試試看,應該沒問題才對):
//如果wifi和gps都沒值的話,先開一下gps,看有沒有gps值,沒有的話用自已給的預設值
//如果wifi那邊有值且gps沒值的話,先開一下gps,看有沒有gps值,沒有的話用wifi值
//注意這邊hero和milestone寫法不同
//如果gps有值的話,就用gps值
protected LocationManager mgr;
private String best;
private Location location;
private double lat=25.0402555, lng=121.512377;
//回傳座標
public double[] updateStat(){
mgr =(LocationManager)ctx.getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
best = mgr.getBestProvider(criteria, true);
//Hero 2.1 開啟gps
if(android.os.Build.MODEL.equals("HTC Hero")){
if(mgr.getLastKnownLocation("gps") != null){
mgr.requestLocationUpdates(best, 2000, 0, mLocationListener01);
location = mgr.getLastKnownLocation(best);
lat=location.getLatitude();
lng=location.getLongitude();
}else if (mgr.getLastKnownLocation("network") != null){
mgr.requestLocationUpdates("gps", 2000, 0, mLocationListener01);
location = mgr.getLastKnownLocation(best);
lat=location.getLatitude();
lng=location.getLongitude();
}else{
mgr.requestLocationUpdates(best, 2000, 0, mLocationListener01);
lat=25.0402555;
lng=121.512377;
}
}
//MileStone 開啟gps
else{
if(mgr.getLastKnownLocation("gps") != null){
mgr.requestLocationUpdates(best, 2000, 0, mLocationListener01);
location = mgr.getLastKnownLocation(best);
lat=location.getLatitude();
lng=location.getLongitude();
}else if (mgr.getLastKnownLocation("network") != null){
short i=0;
while(location == null){
mgr.requestLocationUpdates("gps", 2000, 0, mLocationListener01);
location = mgr.getLastKnownLocation(best);
if(i++==1000){
location = mgr.getLastKnownLocation("network");
break;
}
}
lat=location.getLatitude();
lng=location.getLongitude();
}else{
mgr.requestLocationUpdates(best, 2000, 0, mLocationListener01);
lat=25.0402555;
lng=121.512377;
}
}
double[] array_latilongi = {lat,lng};
return array_latilongi;
}
public final LocationListener mLocationListener01 = new LocationListener(){
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.78.145
※ 編輯: meya 來自: 140.114.78.145 (07/26 22:21)
推 terrybob:好文!來推 07/26 22:25
推 tynus:推! 07/26 23:29
推 orzreynold:好感謝這位高手!!!!!!基本上是都解決了,也會自動開啟 07/27 01:05
→ orzreynold:了,但是怎麼樣才可以說直到定位好了才給使用者操作呢? 07/27 01:06
→ orzreynold:不過真的很感謝你阿阿阿阿阿阿~~ 07/27 01:07
→ ericinttu:有高手有推 07/27 01:39