gpt4 book ai didi

android - 如何在 Google API 下检查设备中是否启用了定位服务

转载 作者:太空狗 更新时间:2023-10-29 16:14:19 25 4
gpt4 key购买 nike

我是 Google Api 位置的新手。我实现了一个处理所有 Location 回调的类。现在它在位置服务打开时工作。但如果关闭,我创建了一个函数 LocationAlertDialog() 来处理这个错误。我不明白我应该在我的代码中在哪里给她打电话的问题,因为我不知道我在哪里检查 GPS 或网络提供商是否可用。

这是我的代码:

public class LocationTool implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private Fragment frag;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location mLastLocation;
private boolean mRequestingLocationUpdates;
public OnLocationChangedListener delegate= null;

public LocationTool (Fragment frag, OnLocationChangedListener onLocationChangedListener)
{
mRequestingLocationUpdates = true;
delegate = onLocationChangedListener;
this.frag = frag;
buildGoogleApiClientAndCreateLocationRequest();
}

public boolean ismRequestingLocationUpdates() {
return mRequestingLocationUpdates;
}

public LocationRequest getmLocationRequest() {
return mLocationRequest;
}

public GoogleApiClient getmGoogleApiClient() {
return mGoogleApiClient;
}

public Location getmLastLocation() {
return mLastLocation;
}

public void setmLastLocation(Location mLastLocation) {
this.mLastLocation = mLastLocation;
}

public void buildGoogleApiClientAndCreateLocationRequest() {

if (checkPlayServices()) {
buildGoogleApiClient();
createLocationRequest();
}
}

private synchronized void buildGoogleApiClient() {

mGoogleApiClient = new GoogleApiClient.Builder(frag.getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
if (mGoogleApiClient!=null)
Log.d("mGoogleApiClient!=null", "mGoogleApiClient!=null");
}

private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(Constants.UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(Constants.FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(Constants.DISPLACEMENT);
}

public boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(frag.getActivity());
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, frag.getActivity(), Constants.PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(frag.getActivity().getApplicationContext(), "This device is not supported.", Toast.LENGTH_LONG).show();
frag.getActivity().finish();
}
return false;
}
return true;
}


@Override
public void onConnected(Bundle bundle) {

// Gets the best and most recent location currently available,
// which may be null in rare cases when a location is not available.
Log.d("Connected", "Connected");

if (mRequestingLocationUpdates) {
delegate.startLocationUpdates();
Log.d("startLocationUpdates", "startLocationUpdates");
}

this.setmLastLocation(LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient));
if (mLastLocation != null) {
Log.d("mLastLocation != null", "mLastLocation != null");
delegate.updateUI();
}
}

@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(this.frag.getClass().toString(), "Connection failed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
Log.d("Location services is off","failed");
}

public void LocationAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(frag.getActivity());
alertDialog.setTitle("Location settings");
alertDialog.setMessage("We cannot retrieve your location. Please click on Settings and make sure your Location services is enabled");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
frag.getActivity().startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//tryLocating = false;
dialog.cancel();
}
});
alertDialog.show();
}

/**
* this function get Longitude and Latitude coordinates and send back the real street address.
* @param LATITUDE
* @param LONGITUDE
* @param ctx
* @return
*/
public String getCompleteAddressString(double LATITUDE, double LONGITUDE, Context ctx) {
String strAdd = "";
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");

for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current location address", "" + strReturnedAddress.toString());
} else {
Log.w("My Current location address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current location address", "Can not get Address!");
}
return strAdd;
}

/**
* this function convert real address to geographical coordinates.
* @param strAddress -real address
* @return LatLng object which contain the coordinates
*/
public LatLng getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(frag.getActivity());
List<Address> address;
LatLng p1 = null;

try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new LatLng(location.getLatitude(), location.getLongitude() );
Log.d("coordinates",p1.latitude+""+p1.longitude);

} catch (Exception ex) {
Log.d("Location Exception", "error converting address");
ex.printStackTrace();
}

return p1;
}
}

最佳答案

以下代码检查位置是否启用。如果未启用,它会显示警报对话框。

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch (Exception ex){}
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch (Exception ex){}
if(!gps_enabled && !network_enabled){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Startup.this.startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub

}
});
dialog.show();
}

关于android - 如何在 Google API 下检查设备中是否启用了定位服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com