I'm making an android app that uses device sensors and location. The data from both sources will be used to calcute some things about orientation. So far i created two non-activity classes, one for sensor management and one for location management. The problem is when i want to use something that needs Activity or Context to be done, for example: show the dialog by calling startResolutionForResult()
, and check the result in onActivityResult()
. startResolutionForResult()
needs Activity to show a dialog.
What's the best approach to solve this? I've decided to use context as a field member of each non-activity class, but i'm not sure if it's the best option.
Update Here is a part of my code i'm reffering to.
In onCreate()
method i call checkLocationSettings()
method to check if location is on. Then onResult()
callback fires where i can determine what to do depending on location settings. When location is off, it shows dialog called by
status.startResolutionForResult((Activity)getBaseContext(), REQUEST_CHECK_SETTINGS)
The result of users decision is available in onActivityResult()
which is a part of MainActivity.class
Everything works like a charm, but i'm concernd about (Activity)getBaseContext() in particular.
GoogleServiceLocationProvider.class
public class GoogleServiceLocationProvider extends ContextWrapper implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<LocationSettingsResult> {
private static final int REQUEST_CHECK_SETTINGS = 3;
private static final int UPDATE_INTERVAL = 10 * 1000; // 10 seconds
private static final int DISPLACEMENT = 10; // 10 meters
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Location lastLocation;
private long lastTime;
LocationFinder.LocationUpdateListener locationUpdateListener;
private boolean isListenerRunning;
private LocationSettingsRequest locationSettingsRequest;
public GoogleServiceLocationProvider(Context base) {
super(base);
buildApiGoogleClient();
createLocationRequest();
buildLocationSettingsRequest();
}
private void buildLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
locationSettingsRequest = builder.build();
}
public void checkLocationSettings() {
if(googleApiClient != null && locationSettingsRequest != null){
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(
googleApiClient,
locationSettingsRequest
);
result.setResultCallback(this);
}
}
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult((Activity)getBaseContext(), REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
useGoogleLocationServiceAPI();
}
private void useGoogleLocationServiceAPI(){
googleServiceLocationProvider = new GoogleServiceLocationProvider(this);
googleServiceLocationProvider.checkLocationSettings();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case PLAY_SERVICES_RESOLUTION_REQUEST:
switch(resultCode){
case RESULT_OK:
isGoogleLocationService = true;
useGoogleLocationServiceAPI();
break;
case RESULT_CANCELED:
Toast.makeText(this, getString(R.string.google_location_service_missing),
Toast.LENGTH_SHORT).show();
useAndroidLocationAPI();
break;
}
break;
case REQUEST_CHECK_SETTINGS:
switch(resultCode){
case RESULT_OK:
break;
case RESULT_CANCELED:
Toast.makeText(this, getString(R.string.location_off_message),
Toast.LENGTH_SHORT).show();
break;
}
break;
}
}