获取位置并在Android中获取GPS设置

时间:2014-02-25 12:16:26

标签: android gps

我正在开发一个应用程序,因为我成功获取位置,但在获取位置之前我想检查我的gps设置,如果它没有启用,从我的应用程序显示gps的设置,我写了但它不工作请告诉我在哪里犯了错误

mycode的

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progress = ProgressDialog.show(ContextAsync, "Loading data", "Please wait...");
  locationManagerAsync = (LocationManager) ContextAsync.getSystemService(ContextAsync.LOCATION_SERVICE);

    if (locationManagerAsync.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        providerAsync = LocationManager.GPS_PROVIDER;
    }else if (locationManagerAsync.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        providerAsync = LocationManager.NETWORK_PROVIDER;

    } else if (locationManagerAsync.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
        providerAsync = LocationManager.PASSIVE_PROVIDER;
        //Toast.makeText(ContextAsync, "Switch On Data Connection!!!!", 
Toast.LENGTH_LONG).show();
    }

 else 
 if(!locationManagerAsync.isProviderEnabled(LocationManager.GPS_PROVIDER)&&
  !locationManagerAsync.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

        buildAlertMessageNoGps();
    }

}


@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
    latlongDetails =new LatLongDetails();



    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setCostAllowed(false);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    providerAsync = locationManagerAsync.getBestProvider(criteria, false);


    location = locationManagerAsync.getLastKnownLocation(providerAsync);
    // Initialize the location fields
    if (location != null) {
        //  System.out.println("Provider " + provider + " has been selected.");
        latAsync = location.getLatitude();
        lonAsync = location.getLongitude();



    } else {
        //Toast.makeText(ContextAsync, " Locationnot available",    
       Toast.LENGTH_SHORT).show();
    }



    List<Address> addresses = null;
    GeocoderAsync = new Geocoder(ContextAsync, Locale.getDefault());
    try {
        addresses = GeocoderAsync.getFromLocation(latAsync, lonAsync, 1);

        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getCountryName();
        AddressAsync = Html.fromHtml(
                address + ", " + city + ",<br>" + country).toString();
    } catch (Exception e) {
        e.printStackTrace();
        AddressAsync = "Refresh for the address";
    }



    return null;
}

@Override
protected void onPostExecute(Void result) {  

    super.onPostExecute(result);
    progress.dismiss();
    onLocationChanged(location);
    Log.v("latAsync_lonAsync",latAsync+"_"+lonAsync);
    Intent intentAsync = new Intent(ContextAsync,Emerg.class);
    intentAsync.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
    intentAsync.putExtra("calculated_Lat", latAsync);
    intentAsync.putExtra("calculated_Lon", lonAsync);
    intentAsync.putExtra("calculated_address", AddressAsync);

    ContextAsync.startActivity(intentAsync);

}



@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this);
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(ContextAsync);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                 ContextAsync.startActivity(new    
   Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        });


    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
               }
           });
    final AlertDialog alert = builder.create();
    alert.show();
}

2 个答案:

答案 0 :(得分:0)

在onPreExecute()中尝试使用: ...

否则  if(!locationManagerAsync.isProviderEnabled(LocationManager.GPS_PROVIDER) ||   !locationManagerAsync.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

    buildAlertMessageNoGps();

} ...

答案 1 :(得分:0)

我认为你做得很好,在查看你的代码和问题之后,我得到你只想要GPS的位置。如果是启用那么ok否则显示对话框启用。

为达到目的,你将删除两个条件。

else if (locationManagerAsync.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { providerAsync = LocationManager.NETWORK_PROVIDER; }else if (locationManagerAsync.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) { providerAsync = LocationManager.PASSIVE_PROVIDER; //Toast.makeText(ContextAsync, "Switch On Data Connection!!!!", Toast.LENGTH_LONG).show(); }

因此你不会得到gps启用弹出对话框。

我认为它会起作用。