Android 如何处理允许用户在不离开应用程序的情况下启用位置的对话框的结果

Android 如何处理允许用户在不离开应用程序的情况下启用位置的对话框的结果,android,Android,查询1:如果未启用位置,我将在启动屏幕上检查用户的位置。此时会出现一个提示,将在不离开应用程序的情况下启用用户的位置。但当提示显示时,它不是在等待用户的响应(是/否)。为此,我必须将提示结果处理为某种方法,以便根据用户的响应执行其余代码。我曾使用onActivityresult()方法尝试过这个方法,但没有成功,因为首先它需要调用startActivityForResult(),并且它将意图作为其参数之一(在我的代码中,一旦用户说“是”,则无需用户导航手机位置设置,位置将自动启用)。我想在用户单

查询1:如果未启用位置,我将在启动屏幕上检查用户的位置。此时会出现一个提示,将在不离开应用程序的情况下启用用户的位置。但当提示显示时,它不是在等待用户的响应(是/否)。为此,我必须将提示结果处理为某种方法,以便根据用户的响应执行其余代码。我曾使用onActivityresult()方法尝试过这个方法,但没有成功,因为首先它需要调用startActivityForResult(),并且它将意图作为其参数之一(在我的代码中,一旦用户说“是”,则无需用户导航手机位置设置,位置将自动启用)。我想在用户单击yes后执行代码

查询2当对话框显示时,我希望启动屏幕在后台运行

启动屏幕

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);
    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) {
        startLocationAlert = new StartLocationAlert(this);
        Boolean b3=startLocationAlert.settingsrequest();
    } 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();
}
StartOccationalERT

Activity context;
boolean result1=false;
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
GoogleApiClient googleApiClient;
public StartLocationAlert(Activity context) {
    this.context = context;
    googleApiClient = getInstance();

    if(googleApiClient != null){
        settingsrequest();
        googleApiClient.connect();
        result1=true;

    }
}
public  GoogleApiClient getInstance(){
    GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
    return mGoogleApiClient;
}
public boolean 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:
                    result1=true;
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

                    try {
                        status.startResolutionForResult(context, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.e("Applicationsett",e.toString());
                    }
                    result1=true;
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Toast.makeText(context, "Location is Enabled", Toast.LENGTH_SHORT).show();
                    result1=true;
                    break;
            }
        }
    });
    return result1;
}
活动上下文;
布尔结果1=假;
受保护的静态最终整数请求检查设置=0x1;
GoogleapClient GoogleapClient;
公共职业ERT(活动背景){
this.context=上下文;
GoogleAppClient=getInstance();
if(googleApiClient!=null){
设置请求();
googleApiClient.connect();
结果1=真;
}
}
公共GoogleAppClient getInstance(){
GoogleAppClient MgoogleAppClient=新建GoogleAppClient.Builder(上下文).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
返回mgoogleapclient;
}
公共布尔设置请求()
{
Log.e(“settingsrequest”、“Comes”);
LocationRequest LocationRequest=LocationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
位置请求设置间隔(30*1000);
locationRequest.SetFastTestInterval(5*1000);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);//这是关键要素
Pendingreult结果=
LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
最终状态状态=result.getStatus();
最终位置SettingsStates状态=结果。getLocationSettingsStates();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
结果1=真;
打破
案例位置设置StatusCodes.RESOLUTION_要求:
试一试{
status.startResolutionForResult(上下文、请求检查设置);
}catch(IntentSender.sendtintentexe){
Log.e(“Applicationsett”,e.toString());
}
结果1=真;
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
Toast.makeText(上下文,“位置已启用”,Toast.LENGTH_SHORT).show();
结果1=真;
打破
}
}
});
返回结果1;
}