在asynctask android中的onLocationChanged

时间:2015-06-22 05:06:05

标签: android gps location

我需要自动获取用户不断变化的位置。在我的代码中,我必须在asynctask中使用requestLocationUpdates方法。但是从不调用onLocationChanged方法。

这是我的代码

private class GetDetails extends AsyncTask<Void, Void, Void>{


   public TrackLocationListener trackloc;
   public LocationManager locationManager;
   @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("Pre Execute");
        trackloc = new TrackLocationListener();
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,trackloc);
    } 

    @Override
    protected Void doInBackground(Void... arg0) {

        ServiceHandler sh = new ServiceHandler();

        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {

                JSONObject jsonObj = new JSONObject(jsonStr);
                job = jsonObj.getJSONArray("USERDETAILS");

                //**Array of PERSONAL DETAILS**

                for (int i = 0; i < job.length(); i++) {
                    JSONObject c = job.getJSONObject(i);

                   latitude = c.getString("latitude");
                   longitude = c.getString("longitude");

                   System.out.println("LAT 2 "+latitude);
                   System.out.println("LON 2 "+longitude);  
                }  

                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable(){

                    @Override
                    public void run() {
                        System.out.println("Changr Latitude "+change_latitude);
                        System.out.println("Changr Longititude "+change_longitude);
                        final LatLng TutorialsPoint = new LatLng(Double.parseDouble(latitude) , Double.parseDouble(longitude));
                          try { 
                                if (googleMap == null) {
                                    System.out.println("MAP IN");
                                   googleMap = ((MapFragment) getFragmentManager().
                                   findFragmentById(R.id.map)).getMap();
                                }
                             googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                             googleMap.addMarker(new MarkerOptions().position(TutorialsPoint).title(name));
                             googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                                     Double.parseDouble(latitude), Double.parseDouble(longitude)), 18.0f));

                          } catch (Exception e) {
                             e.printStackTrace();
                          }

                    } 
                   // your UI code here 
                });

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

}

public class TrackLocationListener implements LocationListener{
   TrackLocationListener(){
       System.out.println("Testing ....");
   }
   @Override
   public void onLocationChanged(Location location) {
       System.out.println("Changing....");
       int lat = (int) location.getLatitude(); // * 1E6);
       int log = (int) location.getLongitude(); // * 1E6);
       int acc = (int) (location.getAccuracy());

       String info = location.getProvider();
       try{
           //change_latitude = location.getLatitude();
           //change_longitude = location.getLongitude();
       }catch (Exception e) {
           // progDailog.dismiss();
           // Toast.makeText(getApplicationContext(),"Unable to get Location"
           // , Toast.LENGTH_LONG).show();
       }

   }

   public void onProviderDisabled(String provider) {
       Log.i("OnProviderDisabled", "OnProviderDisabled");
   }

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

   public void onStatusChanged(String provider, int status,
           Bundle extras) {
       Log.i("onStatusChanged", "onStatusChanged");

   }

}

0 个答案:

没有答案