私はGoogleApiの場所全体で新しいです。そして、すべての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();
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加