onLocationChanged是否会被调用,即使它不在Service的onStart中?

时间:2016-03-30 20:19:55

标签: java android locationlistener

我有一个服务,需要在用户移动时获取用户的位置。我还没有弄清楚如何在onStart中放置onLocationChanged,所以我把它放在外面。当用户移动时它仍会被调用吗?如果没有,我将如何在onStart中调用它?

public class myService extends Service implements LocationListener {
Double lat;
Double lng;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();





return START_STICKY;

}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

}



@Override
public void onLocationChanged(Location location) {

if (MainActivity.location != null) {


   lat = MainActivity.location.getLatitude();

    lng = MainActivity.location.getLongitude();


    if (MainActivity.placeArrayList.size() != 0) {
        for (int i = 0; i < MainActivity.placeArrayList.size(); i++) {


            Log.e("hello", String.valueOf(Float.parseFloat(MainActivity.latitudeArrayList.get(i))));

            MainActivity.destination.setLatitude(Double.parseDouble(MainActivity.latitudeArrayList.get(i)));
            MainActivity.destination.setLongitude(Double.parseDouble(MainActivity.longitudeArrayList.get(i)));
            Log.e("distancemeters", String.valueOf(MainActivity.location.distanceTo(MainActivity.destination)));

            if (MainActivity.location.distanceTo(MainActivity.destination)<100) {




                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

                audioManager.setStreamVolume(AudioManager.STREAM_RING, AudioManager.RINGER_MODE_SILENT, AudioManager.FLAG_SHOW_UI);


            } else {



            }

        }


    }
}

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

1 个答案:

答案 0 :(得分:0)

假设您使用的是Google FusedLocationProviderApi,您需要先做几件事:

  1. 更改您的课程以实施GoogleApiClient.ConnectionCallbacksGoogleApiClient.OnConnectionFailedListener
  2. 创建您的GoogleApi对象
  3. 创建您的LocationRequest对象
  4. 将您的课程注册为LocationListener
  5. 首先,按如下方式更改类定义:

    public class myService extends Service implements LocationListener,      
                                                      GoogleApiClient.ConnectionCallbacks, 
                                                      GoogleApiClient.OnConnectionFailedListener {
    

    然后将其放入onStart()方法:

    // 2. Create the GoogleApi object
    GoogleApi = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    
    // 3. Create the LocationRequest
    mLocationRequest.setInterval(1000); // in milliseconds
    mLocationRequest.setFastestInterval(1000); // in milliseconds
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    
    // 4. Register your listener
    LocationServices.FusedLocationApi.requestLocationUpdates(GoogleApi, mLocationRequest, this)
    

    如果您要取消注册您的听众,请将其放在onDestroy()

    LocationServices.FusedLocationApi.removeLocationUpdates(GoogleApi, this);
    

    但是,请记住,使用此方法意味着您将在指定的时间间隔内获得位置更新,当用户移动时,。您需要找到一种方法来跟踪用户的位置并确定构成&#34;运动的最小距离&#34;。此外,您还需要考虑该位置可能会围绕用户的位置跳舞,因此您还必须考虑到这一点。