【问题标题】:Have latitude and longitude in a service return 0.0服务中的经纬度返回 0.0
【发布时间】:2017-12-25 04:19:40
【问题描述】:

我是 Android 应用程序开发 的初学者。我将每 5 分钟从 GPSTracker.class 获取 latitudelongitude 以在下一步发送到 SQLite DB 但是当我致电 GPSTracker.class 它也返回 latitude = 0.0longitude = 0.0

请帮帮我

这是我的代码

AppService.class

    public class AppService extends Service {
    private MyThread thread;
    PowerManager.WakeLock wl;
    double latitude; 
    double longitude; 

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        thread = new MyThread();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (!thread.isAlive()) {      
            thread.start();
        }
        return START_STICKY;   
    }

    private class MyThread extends Thread {  
        private static final String tag = "Sevice Demo";
        private static final int delay = 300000;
        private int roundNumber = 0;
        private boolean finishService = false;

        @Override
        public void run() {
            while (true) {
                PowerManager pm = (PowerManager) getApplicationContext().getSystemService(
                        getApplicationContext().POWER_SERVICE);
                wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
                wl.acquire();
                wl.release();

                GPSTracker gps = new GPSTracker(AppService.this);
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                Log.d("dddd",String.valueOf(latitude)+" & "+String.valueOf(longitude));

                try {
                    sleep(delay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (finishService) {
                    return;
                }
            }
        }
    }

    @Override
    public void onDestroy() {  
        Toast.makeText(this, "Service Stop...", Toast.LENGTH_LONG).show();
        if (thread.isAlive()) {     
            stopService(new Intent(this, AppService.class));
        }
        super.onDestroy();
    }

    public class GPSTracker extends Service implements LocationListener {
        private final Context mContext;
        boolean isGPSEnabled = false;          
        boolean isNetworkEnabled = false;     
        boolean canGetLocation = false;    
        Location locations;

        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
        private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 
        protected LocationManager locationManager;         

        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }

        public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(LOCATION_SERVICE);

                // get GPS status
                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);

                // get network status
                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                if (!isGPSEnabled && !isNetworkEnabled) {
                    // no network provider is enabled
                } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            //return TODO;
                        }
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                    }
                    if (locationManager != null) {
                        locations = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                    if (locations != null) {
                        latitude = locations.getLatitude();
                        longitude = locations.getLongitude();
                    }

                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (locations == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            locations = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (locations != null) {
                                latitude = locations.getLatitude();
                                longitude = locations.getLongitude();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return locations;
        }

        public double getLatitude() {
            if (locations != null) {
                latitude = locations.getLatitude();
            }
            return latitude;
        }

        public double getLongitude() {
            if (locations != null) {
                longitude = locations.getLongitude();
            }
            return longitude;
        }

        public boolean canGetLocation() {
            return this.canGetLocation;
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onLocationChanged(Location location) {

        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }

}

【问题讨论】:

  • 使用 googleApiClient 获取位置,networkProvider 在建筑区域无法正常工作。
  • 也添加您的清单文件
  • 您应该使用FusedLocationProviderAPI 来获取准确的位置。
  • 感谢回复,我会努力的:)

标签: java android android-service latitude-longitude


【解决方案1】:

融合位置提供程序是 Google Play 服务中的一个位置 API,它智能地组合不同的信号以提供您的应用所需的位置信息。

融合位置提供程序管理底层位置技术,例如 GPS 和 Wi-Fi,并提供一个简单的 API,您可以使用该 API 来指定所需的服务质量。例如,您可以请求获得最准确的数据,或者在不增加功耗的情况下获得尽可能高的准确度。

Last Known Location

Receiving Location Updates

【讨论】:

  • 您使用的代码已经过时,因此不推荐用于测量和更新位置数据/
【解决方案2】:

这段代码

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MIN_TIME_BW_UPDATES,
            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    Log.d("GPS Enabled", "GPS Enabled");
    if (locationManager != null) {
        locations = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (locations != null) {
            latitude = locations.getLatitude();
            longitude = locations.getLongitude();
        }
    }

需要重构。当您致电 LocationManager.requestUpdates() 时,您是在要求 LocationManager 在 GPS 位置更新时回拨您的应用程序。这将在稍后的某个时间(将来)发生,具体取决于手机使用 GPS 确定其位置所需的时间。对LocationManager.getLastKnownLocation() 的调用只会返回最后一个“已知”位置,它还没有。

一旦您请求位置更新,您的应用就会被回调并提供 GPS 数据。框架将调用方法onLocationChanged()。您目前在该方法中没有代码,因此在调用该方法时您不会做任何事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    相关资源
    最近更新 更多