android - 无法使用 GPS 找到当前位置

标签 android eclipse gps

我尝试过以下程序。它正在 eclipse 中工作 -> 如果你通过 ddms 给出纬度和经度值意味着它在模拟器中显示为当前位置.... 但它没有检测到 Android 手机中的当前位置。

private class mylocationlistener implements LocationListener {

    public void onLocationChanged(Location location) {    
     Date today = new Date();  
Timestamp currentTimeStamp = new Timestamp(today.getTime());

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();    
boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 

          if (isGPS){
              lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
              if (location != null) {


        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        String str = "\n CurrentLocation: "+
        "\n Latitude: "+ location.getLatitude() + 
        "\n Longitude: " + location.getLongitude() + 
        "\n Accuracy: " + location.getAccuracy() + 
        "\n CurrentTimeStamp "+ currentTimeStamp;         
          Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
          tv.append(str);           
                   }      
        else
           {
        String s1="GPS activation in process";
            Toast.makeText(MainActivity.this,s1,Toast.LENGTH_SHORT).show();
            /*alert.setTitle("gps");
            alert.setMessage("GPS activation in progress,\n Please click after few second.");
            alert.setPositiveButton("OK", null);
            alert.show();*/

           }
          }
            else
           {
               String s2="Enable Gps";
            Toast.makeText(MainActivity.this,s2,Toast.LENGTH_SHORT).show();
           }

    } 





public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

最佳答案

我为此提供了一项服务。使用它可以轻松获取经度/纬度。

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

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

当您想要启动时,请在您的 Activity 中复制此代码:

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

创建一类全局常量:

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

当您想要项目中的当前纬度和经度时,只需编写此 globalconstant.lat ,globalconstant.lon

在 list 中添加uses-permission

关于android - 无法使用 GPS 找到当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12243560/

相关文章:

android - 如何以完整 HTML 格式在 WebView 中显示来自 feedburner 的内容?

Android fragment 与之前的 View 和按钮监听器重叠

ios - 快速返回 GPS 位置的实用程序类?

Android Python 获取gps状态

java - 如何设置maven项目?

java - 无法在我的 Android 设备上获取经度和纬度

android - 如何在android中处理多个推送通知

java - Android Spinner 获取所选项目

java - 无法将 Maven 依赖项添加到我的 Eclipse Web 部署程序集中

python - [Python][Pydev]未知编码: MS950_HKSCS