如何让用户在启动画面上启用该位置?
我的代码中有一个对话框,可以在应用启动时启用用户的位置。
当应用程序启动时,第一个启动画面会在几秒钟内显示,如果用户的位置被禁用,则会弹出一个让用户启用位置,但此时对话框后面没有背景
如果用户的位置被禁用,我想同时加载对话框和启动画面(在后台启动画面)。
有什么想法吗?
String status = null;
Bundle bundle = null;
StartLocationAlert startLocationAlert;
protected static final int REQUEST_CHECK_SETTINGS = 1;
GoogleApiClient googleApiClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
googleApiClient = getInstance();
bundle = getIntent().getExtras();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Boolean b1= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
LocationManager locationManager1 = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Boolean b2=locationManager1.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!b1&&!b2) {
final Thread thread = new Thread() {
@Override
public void run() {
super.run();
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Boolean b3=startLocationAlert.settingsrequest();
Log.e("settingsrequest","Comes");
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(SplashScreen.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.e("Applicationsett",e.toString());
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Toast.makeText(SplashScreen.this, "Location is Enabled", Toast.LENGTH_SHORT).show();
break;
}
}
});
finish();
}
}
};
// startLocationAlert = new StartLocationAlert(this);
thread.start();
} else {
startActivityThread();
}
}
public void startActivityThread(){
final Thread thread = new Thread() {
@Override
public void run() {
super.run();
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = null;
boolean loggedIn = false;
SharedPreferences w = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor share = w.edit();
loggedIn = (w.getBoolean("loggedIn", loggedIn));
System.out.println("logged in " + loggedIn);
if (loggedIn) {
intent = new Intent(SplashScreen.this, TestNavigationFragments.class);
} else {
intent = new Intent(SplashScreen.this, Login.class);
}
startActivity(intent);
finish();
}
}
};
thread.start();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
switch (resultCode) {
case Activity.RESULT_OK:
Intent intent = null;
boolean loggedIn = false;
SharedPreferences w = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor share = w.edit();
loggedIn = (w.getBoolean("loggedIn", loggedIn));
System.out.println("logged in " + loggedIn);
if (loggedIn) {
intent = new Intent(SplashScreen.this, TestNavigationFragments.class);
} else {
intent = new Intent(SplashScreen.this, Login.class);
}
startActivity(intent);
finish();
break;
case Activity.RESULT_CANCELED:
break;
default:
break;
}
break;
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public GoogleApiClient getInstance(){
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
return mGoogleApiClient;
}