【问题标题】:Why location service stopped to send location updates when i locked phone为什么当我锁定手机时位置服务停止发送位置更新
【发布时间】:2018-09-01 13:18:07
【问题描述】:

我已经创建了后台服务来定期获取用户位置并将其发送到服务器,如下所示:`

public class SyncDataToServerService extends Service {

    private static final String TAG = "SyncDataToServerService";
    private ServerCall serverCall;
    private SharedPreferences tempPref2;
    private Timer timer;
    LocationManager locationManager;
    UtilityClass utilityClass;
    PowerManager.WakeLock wakeLock;
    PowerManager powerManager;

    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");

        serverCall = new ServerCall(this);
        timer = new Timer();
        utilityClass = new UtilityClass(this);
        tempPref2 = getSharedPreferences(getString(R.string.temp_pref_name_2), Context.MODE_PRIVATE);

    }

    @SuppressLint("MissingPermission")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.e(TAG, "onStartCommand");

        powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        if (powerManager != null)
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "MyApp::MyWakelockTag");
        wakeLock.acquire();

        //initialize and start Location service
        startLocation();

        //initialize and start the TimerTask's job
        startTimer();

        return START_STICKY;
    }

    @SuppressLint("MissingPermission")
    private void startLocation() {

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (locationManager != null)
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    10, new MyLocationListener());

        if (locationManager != null)
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                    10, new MyLocationListener());

    }


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

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        if (locationManager != null)
            locationManager.removeUpdates(new MyLocationListener());
        super.onDestroy();
    }

    private void syncPath(Location mLocation) {

        Log.w(TAG, "Location: " + mLocation + "  Latitude: " + mLocation.getLatitude() + " & Longitude: "
                + mLocation.getLongitude());

        if (tempPref2.getString(getString(R.string.attendance_key), "").equalsIgnoreCase("present")) {

            serverCall.sendEmpPath(mLocation);
        }
    }

    private void startTimer() {

        TimerTask timerTask = new TimerTask() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            public void run() {

                if (utilityClass.isInternetConnected()) {

                    if (serverCall != null) {

                        if (serverCall.requestQ.size() > 0)
                            Log.e(TAG, "Request queue size ==" + serverCall.requestQ.size());

                        serverCall.syncData();
                    }
                }

            }
        };

        //schedule the timer, to wake up every 1 second
        timer.schedule(timerTask, 1000, 1000);

    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            Log.e(TAG, "onLocationChanged: " + location);

            Intent intent1 = new Intent("com.example.mc_project");
            intent1.putExtra("location", location);
            sendBroadcast(intent1);

            if (location.getAccuracy() < 25)
                syncPath(location);

            wakeLock.release();
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    }
}`

一切正常,但是当我锁定手机时,它停止发送位置更新。我已经读到这是因为系统睡眠并且必须使用唤醒锁但它也不起作用。当我解锁手机时,它应该开始发送位置更新,但这也没有发生。我的服务不会停止,它总是在运行状态。请在我做错或遗漏的地方提供帮助。

【问题讨论】:

    标签: android service location


    【解决方案1】:

    您应该使用前台服务并将位置更新代码放入其中。 os 在任何时候断电都会停止后台服务

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2012-03-17
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      相关资源
      最近更新 更多