Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓GPS按钮点击时启用_Android_Gps_Android Alertdialog - Fatal编程技术网

Android 安卓GPS按钮点击时启用

Android 安卓GPS按钮点击时启用,android,gps,android-alertdialog,Android,Gps,Android Alertdialog,这是我的密码。(它使alertdialog移动设置) 但我只想在点击“启用GPS”按钮时启用GPS。 当我运行应用程序而不是只运行一次时,android是否总是启用按钮点击 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Your GPS is disabled! Would you like to enable it?") .setCancelable(false) .setPos

这是我的密码。(它使alertdialog移动设置) 但我只想在点击“启用GPS”按钮时启用GPS。 当我运行应用程序而不是只运行一次时,android是否总是启用按钮点击

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        moveConfigGPS();
    }
}).setNegativeButton("Do nothing",
new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert = builder.create();
alert.show();

您不能仅启用GPS,您只能出于以下目的打开GPS设置:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
更新:

  /**
 * Check if GPS enabled or not
 * and request dialog to enable GPS.
 */
private void checkWhetherLocationSettingsAreSatisfied() {

    LocationRequest mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(1000)
            .setNumUpdates(2);

    final LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    builder.setNeedBle(true);
    SettingsClient client = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            Log.d(TAG, "onSuccess() called with: locationSettingsResponse = [" + locationSettingsResponse + "]");
            hasLocationPermission();

        }
    });
    task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d(TAG, "onSuccess --> onFailure() called with: e = [" + e + "]");
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult(BaseActivity.this,
                            Constants.REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {

                    e.printStackTrace();
                }
            }

        }
    });
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Constants.REQUEST_CHECK_SETTINGS) {

        // Make sure the request was successful
        if (resultCode == RESULT_OK) {

            hasLocationPermission();

        } else {
            //User clicks No
        }
    }

}
/**
*检查GPS是否启用
*和请求对话框以启用GPS。
*/
私人无效检查位置设置是否满足(){
LocationRequest mlLocationRequest=LocationRequest.create()
.setPriority(定位请求。优先级高精度)
.setInterval(1000)
.设定更新日期(2);
最终位置SettingsRequest.Builder=新位置SettingsRequest.Builder().addLocationRequest(MLLocationRequest);
builder.setAlwaysShow(true);
builder.setNeedBle(true);
SettingsClient=LocationServices.getSettingsClient(此);
Task Task=client.checkLocationSettings(builder.build());
task.addOnSuccessListener(这个,新的OnSuccessListener(){
@凌驾
成功时公共无效(位置设置响应位置设置响应){
d(标记“onSuccess(),调用时使用:locationSettingsResponse=[“+locationSettingsResponse+”]);
hasLocationPermission();
}
});
task.addOnFailureListener(这是新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Log.d(标记“onSuccess-->onFailure(),调用时使用:e=[“+e+”]”);
if(可解析APIexception的实例){
//不满足位置设置,但可以修复此问题
//通过向用户显示一个对话框。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
ResolvableApiException resolvable=(ResolvableApiException)e;
可解析。开始解决结果(BaseActivity.this,
常量。请求检查设置);
}catch(IntentSender.sendtintentexe){
e、 printStackTrace();
}
}
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==常量。请求检查设置){
//确保请求成功
if(resultCode==RESULT\u OK){
hasLocationPermission();
}否则{
//用户单击否
}
}
}
我想你想像谷歌一样,只需点击一下就可以打开GPS定位


要提示用户修改位置设置的权限,请调用startResolutionForResult(Activity,int)。此方法将打开一个对话框,请求用户允许修改位置设置

嘿,有没有一种方法可以检测用户何时从上面的对话框中单击“否”按钮???是的。您将在活动中获得resultCode多次尝试,但无论如何都无法获得。感谢您的回复。谢谢,为我工作
  /**
 * Check if GPS enabled or not
 * and request dialog to enable GPS.
 */
private void checkWhetherLocationSettingsAreSatisfied() {

    LocationRequest mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(1000)
            .setNumUpdates(2);

    final LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    builder.setNeedBle(true);
    SettingsClient client = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            Log.d(TAG, "onSuccess() called with: locationSettingsResponse = [" + locationSettingsResponse + "]");
            hasLocationPermission();

        }
    });
    task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d(TAG, "onSuccess --> onFailure() called with: e = [" + e + "]");
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult(BaseActivity.this,
                            Constants.REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {

                    e.printStackTrace();
                }
            }

        }
    });
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Constants.REQUEST_CHECK_SETTINGS) {

        // Make sure the request was successful
        if (resultCode == RESULT_OK) {

            hasLocationPermission();

        } else {
            //User clicks No
        }
    }

}