2016-08-18 86 views
0

我有一個AsyncTask類,當調用AsyncTask時需要獲取用戶的位置。在AsyncTask中獲取GPS位置

想法是檢查位置服務是否啓用,如果沒有,位置服務切換將彈出供用戶打開它,然後它將返回到任務繼續獲取位置並完成作業。

但是,下面的代碼在onPreExecute()的末尾附近顯示NullPointerExceptionlat = location.getLatitude();。他們說,因爲getLastKnownLocation沒有得到任何東西,因爲它剛剛被打開,那麼在位置服務打開後我怎麼才能找到位置?

LocationManager locationManager; 
LocationListener locationListener; 
double lat; 
double longi; 

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    location = new Location(LocationManager.NETWORK_PROVIDER); 
    locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE); 
    if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     context.startActivity(intent); 
    } 

    //I copy the part below from the internet but seems like "onProviderDisabled" and "onLocationChanged" were never be called 
    locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      Log.v("GPS CHECKKKKKKK",location.getLatitude()+"_"+location.getLongitude()); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      context.startActivity(intent); 
     } 
    }; 


    locationManager.requestLocationUpdates("gps", 500, 0, locationListener); 
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,0, locationListener); 
    lat = location.getLatitude(); 
    longi= location.getLongitude(); 
    Log.v("GPS Cord CHECK",lat+"_"+longi); 

} 

@Override 
protected String doInBackground(final Checkout_Package... params) { 
    //Doing the job that needs lat and longi value! 
} 

謝謝你的時間!

回答

1

您不能在onPreExecute()中創建並放置位置偵聽器,因爲只有在onPostExecute或AsyncTask完成時纔會調用onChanged處理程序。

你應該做的是在該位置改變處理程序啓動一個AsyncTask。

所以在onLocationChanged()

+0

你的建議完成了這項工作。謝謝。 –

0

隨着greenapps的建議我想通了。我把代碼放到按鈕中,調用AsyncTask

checkout_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      LocationManager locationManager; 
      LocationListener locationListener; 

      gotit = false; //Boolean to start the AsyncTask only once. 
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      locationListener = new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 
        Log.v("GPS CHECKKKKKKK",location.getLatitude()+"_"+location.getLongitude()); 
        lat = location.getLatitude(); 
        longi = location.getLongitude(); 
        if (gotit == false) { 
         new Checkout_Async(Checkout.this, listener).execute(new Checkout_Package(user, product_all, getTotalCost(product_all), fee, getTotalCost(product_all) + fee, lat, longi)); 
         gotit = true; 
         pd.dismiss(); 
        } 
       } 

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

       } 

       @Override 
       public void onProviderEnabled(String provider) { 

       } 

       @Override 
       public void onProviderDisabled(String provider) { 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent); 

       } 
      }; 
      pd=ProgressDialog.show(Checkout.this,"",getResources().getString(R.string.please_wait),false); 
      locationManager.requestLocationUpdates("gps",50,0,locationListener); 

     } 
    }); 

現在它工作。謝謝!