带有线程的服务上的Android LocationListener

时间:2014-09-26 22:27:49

标签: android locationlistener

我希望以这种方式实现LocationListener

1 - Activity_first - 调用LocationListener,但我有一些微调器,当LocationListeneronStart()时,这不会响应。我想用Thread来实现。

2 - 当调用onLocationChanged()中的LocationListener时,我会调用Geocoder,当地址不同于null时,我会将其保存在共享首选项中,并阻止它。

第2点,必须在后台完成,因为当我获得Activity的地址时用户可能在Geocoder(大约10秒,这取决于连接)

我正在使用IntentService进行测试,但当然,我无法正常使用。

我的代码

public class Carga_1 extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.carga_1);

            // Todo Create Spinner, etc     
            startService(new Intent(Carga_1.this, LocationService2.class));

    }
}

public class LocationService2 extends Service { 
        public static final String BROADCAST_ACTION = "Hello World";
        //public LocationManager locationManager; testted
        //public MyLocationListener listener; tested
        public Location previousBestLocation = null;
        Thread my_thread;


        @Override
        public IBinder onBind(Intent intent) {
            Log.d("[GPS_coord]", "????");
            return null;
        }


        @Override
        public void onCreate() {
            super.onCreate();
        }


        @Override
        public void onStart(Intent intent, int startId) {

            Runnable r = new Runnable() {
                public void run() {
                    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    MyLocationListener listener = new MyLocationListener();
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
                }
            };
            my_thread = new Thread(r);
            my_thread.start();

        }


        @Override
        public void onDestroy() {
            super.onDestroy();
            my_thread.stop();
        }

        private void get_address(double lat, double longi) {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses;
            try {
                addresses = geocoder.getFromLocation(lat, longi, 1);
    //SAVE ON SharedPrefereces
                this.stopSelf();
            }
            catch (IOException e) {
                Log.d("[GPS_geocoder]", "Decodificacion Erronea");
            }

        }

        public class MyLocationListener implements LocationListener {
            public void onLocationChanged(final Location loc) {
                Log.i("GPS_Service", "Location changed");
                Log.d("GPS_Service", String.valueOf(loc.getLatitude() + " : " + String.valueOf(loc.getLongitude())));
                get_address(loc.getLatitude(), loc.getLongitude());
            }

            public void onProviderDisabled(String provider) {
                Log.i("GPS_Service", "DES_habilitado");

            }

            public void onProviderEnabled(String provider) {
                Log.i("GPS_Service", "Habilitado");

            }

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

            }
        }

0 个答案:

没有答案