1

我是 android 开发新手,我了解 android 活动生命周期。请看下面的代码。

    public class MyTest extends Activity{

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        cont = getApplicationContext();
        final MyLocation my_loc = new MyLocation ();
        my_loc.initialize(cont);

        myMethod();
        }

    Public void myMethod()
    {
       //here when I retrieve the values, it always shows 0.0.
    }
}

    public class MyLocation implements LocationListener {
        public double user_lat;
        public double user_lng;
        private LocationManager locationManager;
        private Context ctx;

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            user_lat = location.getLatitude();
            user_lng = location.getLongitude();     

            //here i save the values for constands to use in myMethod                           
            }
        }

        public void initialize(Context context) {
            this.ctx = context;
            locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, this);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    0, this);
        } 
}

为什么我无法在同一活动中初始化 GPS 并检索经纬度值?

4

3 回答 3

3

LocationClient 是获取 GPS 的新方法。观看视频以获取有关最近更新的完整详细信息。

请注意,LocationManager 的处理方式有问题,因为需要为 GPS 和 NETWORK 提供程序添加大量代码。新方法(谷歌内部称为 Fused Location Provider)与传感器一起工作,以大幅减少电池消耗。还减少了对复杂 API 的需求和对最佳提供商的阶段性选择。只需 2-3 行代码,您就完成了。

对于许多三星手机(包括 Y 型号),尽管存在一个特定问题,即大多数时候它们根本不会返回位置。因此,您需要启动该手机以返回 GPS。为此,您可以使用

HomeScreen.getLocationManager().requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onLocationChanged(final Location location) {
        }
    });

然后调用你的locationClient.getLastLocationapi。如上面的代码所示,就在您的 LocationClient.getLastLocation 或 LocationManager.getLastKnownLocation 调用之前。

请注意三星是高度定制的安卓开源产品。谷歌无法响应三星相关的错误,三星也没有任何安卓开发者相关的支持。

编辑:观看视频,相信我,不知道 LocationClient 给了你什么,你不会欣赏这种变化。您还将了解地理围栏。

于 2013-05-29T10:11:15.617 回答
1

为了您的位置问题,我建议您另一种检索位置的新方法。

看看这个:谷歌播放工具

下面的代码只是一个片段,并不完整;)。您不再需要处理 gps/网络提供商。它为您处理,哪个是最好的并返回一个位置。

LocationClient mLocationClient = new LocationClient(...)
mLocationClient.connect();
...
onConnected(Bundle ...){
LocationRequest request = LocationRequest.create();
request.setNumUpdates(1);
...
mLocationClient.requestLocationUpdates(request, new LocationListener....)
....

}
于 2013-05-29T08:04:23.320 回答
0

我为此做了一项服务。使用它很容易获得经度/纬度。

将此类复制/粘贴到您的项目中。

package com.sample;
    import com.sample.globalconstant;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;

    public class MyServiceGPS extends Service
    {
        private static final String TAG = "BOOMBOOMTESTGPS";
        private LocationManager mLocationManager = null;
        private static final int LOCATION_INTERVAL = 1000;
        private static final float LOCATION_DISTANCE = 10f;

        private class LocationListener implements android.location.LocationListener{
            Location mLastLocation;
            public LocationListener(String provider)
            {
                Log.e(TAG, "LocationListener " + provider);
                mLastLocation = new Location(provider);
            }
            public void onLocationChanged(Location location)
            {
                Log.e(TAG, "onLocationChanged: " + location.getLatitude() +"....."+ location.getLongitude());
                globalconstant.lat  = location.getLatitude();
                globalconstant.lon  = location.getLongitude();
                Toast.makeText(getApplicationContext(), location.getLatitude() +"....."+ location.getLongitude(), 1000).show();
                mLastLocation.set(location);
            }
            public void onProviderDisabled(String provider)
            {
                Log.e(TAG, "onProviderDisabled: " + provider);           
            }
            public void onProviderEnabled(String provider)
            {
                Log.e(TAG, "onProviderEnabled: " + provider);
            }
            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                Log.e(TAG, "onStatusChanged: " + provider);
            }
        }
        LocationListener[] mLocationListeners = new LocationListener[] {
                new LocationListener(LocationManager.GPS_PROVIDER),
                new LocationListener(LocationManager.NETWORK_PROVIDER)
        };
        @Override
        public IBinder onBind(Intent arg0)
        {
            return null;
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        {
            Log.e(TAG, "onStartCommand");
            super.onStartCommand(intent, flags, startId);      
            return START_STICKY;
        }
        @Override
        public void onCreate()
        {
            Log.e(TAG, "onCreate");
            initializeLocationManager();
            try {
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[1]);

            } catch (java.lang.SecurityException ex) {
                Log.i(TAG, "fail to request location update, ignore", ex);
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "network provider does not exist, " + ex.getMessage());
            }
            try {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[0]);
            } catch (java.lang.SecurityException ex) {
                Log.i(TAG, "fail to request location update, ignore", ex);
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "gps provider does not exist " + ex.getMessage());
            }
        }
        @Override
        public void onDestroy()
        {
            Log.e(TAG, "onDestroy");
            super.onDestroy();
            if (mLocationManager != null) {
                for (int i = 0; i < mLocationListeners.length; i++) {
                    try {
                        mLocationManager.removeUpdates(mLocationListeners[i]);
                    } catch (Exception ex) {
                        Log.i(TAG, "fail to remove location listners, ignore", ex);
                    }
                }
            }
        }
        private void initializeLocationManager() {
            Log.e(TAG, "initializeLocationManager");
            if (mLocationManager == null) {
                mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            }
        }
    }

当你想开始时,将这段代码复制到你的活动中:

startService(new Intent(this,MyServiceGPS.class));

创建一类全局常量:

public class globalconstant { public static double lat, lon; }

当你想在你的项目中当前的纬度和经度时,只写这个globalconstant.lat ,globalconstant.lon

添加uses-permission in Manifest

于 2013-05-29T07:42:51.890 回答