【问题标题】:GPS not receiving location updates when device goes to sleep(black screen)设备进入睡眠状态时 GPS 未接收到位置更新(黑屏)
【发布时间】:2019-10-02 12:35:15
【问题描述】:

我遇到了一个问题,我有一个跟踪用户位置的活动。它用于跟踪户外活动,但是当设备进入睡眠状态时,GPS 会停止向我的 LocationListener 发送位置更新。

我该如何解决这个问题?或者什么是正确的实现?

我已阅读部分唤醒锁是解决方案,因此我按照说明在 oncreate 和 ondestroy 中的 realeas 中添加了部分唤醒锁,但这对它没有影响。

然后,另一个建议的解决方案是拥有一个前台位置服务,它将这些位置更新发送到我的活动以进行处理。但这似乎太复杂了,而且服务有可能被破坏,这是不可接受的。即使我可以重新启动它,但仍然。

什么是正确的实现? 如有必要,我会使用前台服务来完成,但无论如何,我需要在这里有一些经验的人..

感谢您的建议

【问题讨论】:

  • 使用 ForegroundService 并指示用户在设备设置中删除应用程序的任何电池优化。 (它们可能因制造商而异。)
  • “建议的解决方案是让前台定位服务 [...] 服务被销毁的机会” 实际上,前台服务被销毁的可能性要小得多. “一种意大利面条代码..” 如果你做得对,它就不是意大利面条代码。 :)
  • 谢谢马库斯,这实际上是我需要听到的。可能是电池优化可以阻止sone dev8ces上的更新,显然是较新的,因为如果我记得它很好,它曾经可以工作我的旧手机..
  • 我可以使用前台服务并注册一个接收器,以防服务停止并在我的活动中重新启动它。因为户外远足等可能会持续很长时间。系统可能会关闭它下来谁知道..
  • 我已经尝试过这个实现并且它有效????..感谢您的建议

标签: java android gps


【解决方案1】:

您需要为其创建一个服务才能在没有活动的情况下继续定位

   import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import static android.content.Context.LOCATION_SERVICE;

public class MyLocationListener implements LocationListener {

    public static double latitude;
    Context ctx;
    Location location;
    LocationManager locationManager;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    public static double longitude;
    MyLocationListener(Context ctx) {
        this.ctx = ctx;
        try {
            locationManager = (LocationManager) ctx.getSystemService(LOCATION_SERVICE);
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            Toast.makeText(ctx, "GPS Enable " + isGPSEnabled, Toast.LENGTH_LONG).show();
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            Toast.makeText(ctx, "Network Enable " + isNetworkEnabled, Toast.LENGTH_LONG).show();

            if ( Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission
                    ( ctx, android.Manifest.permission.ACCESS_FINE_LOCATION )
                    != PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission( ctx,
                            android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
                            PackageManager.PERMISSION_GRANTED) {  }
            if (isGPSEnabled == true) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,     0,       0, this);
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
            if (isNetworkEnabled==true) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,    0,     0, this);
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            // Toast.makeText(ctx,"latitude: "+latitude+" longitude: "+longitude,Toast.LENGTH_LONG).show();


        }
        catch(Exception ex)
        {

            Toast.makeText(ctx,"Exception "+ex, Toast.LENGTH_LONG).show();
        }
    }
    @Nullable
    @Override
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();
        latitude=loc.getLatitude();
        longitude=loc.getLongitude();
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        //print "Currently GPS is Disabled";
    }
    @Override
    public void onProviderEnabled(String provider)
    {
        //print "GPS got Enabled";
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {

    }
}

要使用该类,请添加此方法位置存储在地址字符串中

public void getLocation(){
    Double latitude = 0.0, longitude;
    String message = "";
    LocationManager mlocManager = null;
    LocationListener mlocListener;
    mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener(this);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
    }
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

        latitude = MyLocationListener.latitude;
        longitude = MyLocationListener.longitude;
        message = message +"https://www.google.com/maps/dir/@"+ latitude +","+  longitude;
        address=message;
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
        if (latitude == 0.0) {
            Toast.makeText(getApplicationContext(), "Currently gps has not found your location....", Toast.LENGTH_LONG).show();
        }

    } else {
        Toast.makeText(getApplicationContext(), "GPS is currently off...", Toast.LENGTH_LONG).show();
    }
}

【讨论】:

  • 您有多余的代码。 getLocation() 方法请求位置更新并创建MyLocationListener 的实例,该实例也请求位置更新。 MyLocationListener 只听位置更新还不够吗? loc.getLatitude();loc.getLongitude(); 中的 onLocationChanged() 也没有做任何有用的事情。 (只是一些观察。)
  • @Markus Kauppinen,我们可以在这里指导,但无法根据问题需要提供现成的代码,使用此答案后很容易处理进一步的过程
  • 谢谢大家.. 我不会使用前台服务,即使我不喜欢这种方法.. 简单中的美..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 2012-01-17
  • 2023-04-01
  • 1970-01-01
  • 2013-11-18
  • 2015-10-10
相关资源
最近更新 更多