1. 程式人生 > >Android保證首次獲取到的location物件不為空的解決方案

Android保證首次獲取到的location物件不為空的解決方案

package com.example.locationtest;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    LocationManager locationManager;
    TextView textView;
    Location location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        // 獲取location物件
        location = getBestLocation(locationManager);

        updateView(location);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000, 8, new LocationListener() {

                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                        updateView(locationManager
                                .getLastKnownLocation(provider));
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        updateView(null);
                    }

                    @Override
                    public void onLocationChanged(Location location) {
                        location = getBestLocation(locationManager);// 每次都去獲取GPS_PROVIDER優先的location物件
                        updateView(location);
                    }
                });
    }

    private void updateView(Location location) {
        if (location != null) {
            StringBuffer sb = new StringBuffer();
            sb.append("位置資訊:\n");
            sb.append("經度:" + location.getLongitude() + ", 緯度:"
                    + location.getLatitude());
            textView.setText(sb.toString());
        } else {
            textView.setText("");
        }
    }

    /**
     * 獲取location物件,優先以GPS_PROVIDER獲取location物件,當以GPS_PROVIDER獲取到的locaiton為null時
     * ,則以NETWORK_PROVIDER獲取location物件,這樣可保證在室內開啟網路連線的狀態下獲取到的location物件不為空
     * 
     * @param locationManager
     * @return
     */
    private Location getBestLocation(LocationManager locationManager) {
        Location result = null;
        if (locationManager != null) {
            result = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (result != null) {
                return result;
            } else {
                result = locationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                return result;
            }
        }
        return result;
    }

}
佈局檔案如下: