即使手机被锁定,也会接收位置更新?

时间:2018-04-04 15:28:17

标签: java android android-service google-api-client fusedlocationproviderapi

当我的手机被锁定时,我一直在尝试接收位置更新我正在使用由我的主要活动启动的服务。当应用程序打开时,我收到位置更新,但是当锁定电话时,服务继续运行,但每次存储的位置都相同。

import static com.example.kromby.mapsapplication.featureOptions.date;


public class BackgroundLocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks, LocationListener {

// I've removed code that does not relate to the question so please ignore most imports. 

    private GoogleApiClient mClient;

    private boolean connected = false;

    private LocationRequest mLocationRequest;

    public static List<LatLng> globalLatLng = Collections.synchronizedList(new ArrayList<LatLng>());

    public static GoogleMap mMap;

    Location mLastLocation;

    private static double latitude;

    private static double longitude;

    public LatLng mLatLng = new LatLng(latitude, longitude);

    private static final String TAG = "MapActivity";

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

    @TargetApi(Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Runnable r = new Runnable() {

            @Override
            public void run() {
                buildGoogleApiClient();
            }
        };

        Runnable q = new Runnable() {

            @Override
            public void run() {

                long p = System.currentTimeMillis() + 4000;

                boolean continueLooping = true;

                while (continueLooping) {

                    if (System.currentTimeMillis() >= p) {

                        locationUpdates();

                        continueLooping = false;
                    }
                }
            }
        };

        Thread mThread = new Thread(r);

        Thread mThreadTwo = new Thread(q);

        mThread.start();

        mThreadTwo.start();

        return Service.START_STICKY;
    }

    public void locationUpdates() {

        long i = System.currentTimeMillis();

        while (true) {

              if (System.currentTimeMillis() == i) {

                  i = System.currentTimeMillis() + 3000;

                  // Checks that the application has user permissions to location.

                  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
                      && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      return;
                  }

                  mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mClient);

                  // Once the phone is locked this will return the same location each time and not change.

                  Log.d(TAG, "Client is working " + mClient.isConnected() + "\n Current mLastLocation LatLng " + mLastLocation.getLatitude() + "," + mLastLocation.getLatitude());

                  latitude = mLastLocation.getLatitude();

                  longitude = mLastLocation.getLongitude();

                  Log.d(TAG, "latitude = " + String.valueOf(latitude) );

                  mLatLng = new LatLng(latitude, longitude);

                  globalLatLng.add(mLatLng);

                  Log.d(TAG, "Global List = " + globalLatLng);

            }
        }
    }

    protected synchronized void buildGoogleApiClient() {

        mClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API).build();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        mClient.connect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        connected = true;

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(3000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }

        LocationServices.FusedLocationApi.requestLocationUpdates(mClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {

        mClient.connect();
    }

    @Override
    public void onDestroy() {

        Log.i(TAG, "OnDestroy method called");
    }
}

我的目标是将一个位置保存到Latlng列表中并将其存储到文本文件中,该文件可以在折线中读取并显示在地图上。我在两个单独的线程上运行它们因为googles api客户端没有完成所以它不会到达任何其他代码。

0 个答案:

没有答案