【问题标题】:Android cant access location in the backgroundAndroid无法在后台访问位置
【发布时间】:2022-01-19 15:12:27
【问题描述】:

我的应用有问题 它应该每 20-30 秒将当前位置发送到服务器,并且它曾经可以工作。 但是我们刚刚将它安装在使用 android 8.0 的新手机上,在这里我可以读到有一些背景位置限制,但我不知道如何让它们更快地获得一些更新。 atm 如果显示屏关闭,我每小时会收到 3-4 次更新

到目前为止,我正在像这样启动我的后台服务

intentService = new Intent(getBaseContext(), BackgroundService.class);
    mLayout = findViewById(R.id.layout);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Log.e(TAG, "onCreate: Startet as foregroundservioce" );
        startForegroundService(intentService);
    } else {
        Log.e(TAG, "onCreate: started as background service" );
        startService(intentService);
    }

后台服务onStartCommand()

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Let it continue running until it is stopped.
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        appPreferences = new AppPreferences(this.getBaseContext());
        child = appPreferences.getChild();
        lockStatic = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "taxaapp:WorkInBackground123");
        lockStatic.acquire();
        sslClientService = new SSLClientService(this);
        return START_STICKY;
    }

我的地理位置管理器

public class GeoLocationService {
    private Timer mTimer;
    private Alarm alarm;
    private boolean tracking;
    private LocationManager locationManager;
    private BackgroundService _context;
    private Child child;
    private SSLClientService sslClientService;
    private long alarmTimeToStop = 0;
    private LocalDatabaseHandler ldbh;
    private Handler mHandler = new Handler();

    /**
     * constructor initializes
     *
     * @param context
     * @param child
     */
    public GeoLocationService(BackgroundService context, Child child) {
        _context = context;
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        sslClientService = _context.getSSLClient();
        ldbh = new LocalDatabaseHandler(context);
        mTimer = new Timer();
        this.child = child;
    }

    /**
     * Updates the working alarm
     *
     * @param alarm
     */
    public void setAlarm(Alarm alarm) {
        if (this.alarm.getId() == 0) {
            this.alarm = alarm;
        }
        _context.sendMessageToUI(R.integer.MSG_GPS_START);
    }

    /**
     * Returns the alarm being used at the moment
     */
    public Alarm getAlarm() {
        return alarm;
    }

    /**
     * Starts location listener
     *
     * @return true if it was able to start
     */
    public boolean start() {
        boolean bool = false;
        mTimer.schedule(new TimedTask(), 20, 30000);
        if (!isTracking()) {
            alarm = new Alarm(child.getId());
            alarmTimeToStop = System.currentTimeMillis() / 1000L + _context.getResources().getInteger(R.integer.GpsMaxTime);
        } else {
            if (!alarm.getAlarmCoordinates().isEmpty()) {
                alarm = new Alarm(child.getId());
                alarmTimeToStop = System.currentTimeMillis() / 1000L + _context.getResources().getInteger(R.integer.GpsMaxTime);
//                sslClientService.SendAlarm(new Alarm(child.getId(), alarm.getLastAlarmCoordinates().getAddress()));
            }
        }
        // Register the listener with the Location Manager to receive location updates
        if (ActivityCompat.checkSelfPermission(_context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(_context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(_context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.e("Missing permissions", "we are missing some permissions for GPS location");
            bool = false;
        }
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, _context.getResources().getInteger(R.integer.GeoSettingsDistance), locationListener);
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, _context.getResources().getInteger(R.integer.GeoSettingsDistance), locationListener);
            locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListener);
            tracking = true;
            bool = true;
            ldbh.addLog("Starting new alarm", 200, "GeoLocationService-Start");
        }
        return bool;
    }

    /**
     * is the tracking active
     */

    /**
     * Listens for location updates
     */
    private LocationListener locationListener = new LocationListener() {
        private long time = 0;


        /**
         * Called when the location has changed.
         * <p> only accepts new location if 10seconds has passed and location accuracy is less than 30
         * <p> There are no restrictions on the use of the supplied Location object.
         *
         * @param location The new location, as a Location object.
         */
        @Override
        public void onLocationChanged(Location location) {
            Log.i(TAG, "onLocationChanged: Location Accuracy and provider: " + location.getAccuracy() + " / " + location.getProvider());
            Log.i(TAG, "onLocationChanged: TIME! " + (location.getTime() - time));
            if (location.getTime() - time > _context.getResources().getInteger(R.integer.GeoSettingsTime) && location.getAccuracy() <= _context.getResources().getInteger(R.integer.GpsAccuracy)) {
                time = location.getTime();
                // Called when a new location is found by the network location provider.
                List<Address> address;
                double lat = location.getLatitude();
                double lng = location.getLongitude();
//                lat = 10.27657397;  //Kan bruges til test af dårlige kordinator
//                lng = 56.21009964;  //Kan bruges til test af dårlige kordinator
                AlarmCoordinates alarmCoordinates = new AlarmCoordinates(lat, lng, alarm.getId());
                
                Log.i(TAG, "onLocationChanged: new coordinates!");
                NewAlarmCoordinates(alarmCoordinates);
            }
        }

【问题讨论】:

    标签: android android-location android-background


    【解决方案1】:

    您处于打盹模式。 https://developer.android.com/training/monitoring-device-state/doze-standby 关闭屏幕后,设备将很快进入打盹,它只会每15分钟左右向应用程序提供事件(高优先级推送消息和警报除外)。这是预期的行为。

    关闭它的唯一方法是让您的用户将您的应用列入电池优化白名单。它不能以编程方式完成。并且不要期望许多用户实际上会这样做。实际上,您需要围绕它编写代码,并在屏幕关闭时进行少量位置更新。

    您也可以使用 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,但在 Play 商店中限制使用,您需要申请豁免。

    【讨论】:

    • hmm 我会试着调查一下,我的应用不会出现在 Play 商店中,并且只安装在我可以访问自己的手机上,所以白名单不会成为问题
    猜你喜欢
    • 1970-01-01
    • 2021-02-08
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多