如何设置onLocationChanged以在我的服务中运行?

时间:2016-03-29 18:13:56

标签: java android

我有一项服务需要运行onLocationChanged。当我尝试将onLocationChanged方法放在onStartCommand中时,它不起作用。如何在服务启动时将onLocationChanged设置为运行?

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", Stri ng.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) {

}
}

0 个答案:

没有答案