我目前正在尝试创建一个简单的,以便当布尔值设置为false时,应用程序的GoogleAPIClient不会连接。应用忽略布尔值?
但是,即使在布尔值设置为false之后,出于某种原因,应用程序仍然会经过并连接。我该如何解决这种错误?
关于此错误的代码片段是以下:
protected void onStart() {
super.onStart();
//Build resources if null
if (mLocationClient == null) {
buildLocationClient();
}
if (settings == null) {
settings = getSharedPreferences(PREFS_NAME, 0);
}
Boolean RequestingLU = settings.getBoolean("RequestingLU", true);
if (RequestingLU) {
mLocationClient.connect();
Toast.makeText(this, "You have connected", Toast.LENGTH_LONG).show();
}
else {
mLocationClient.disconnect();
Toast.makeText(this, "Don't connect please", Toast.LENGTH_LONG).show();
StoppedMessage();
}
}
这是buildLocationClient():
protected void buildLocationClient() {
mLocationClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
这是从另一活性的AlertDialog编辑设置
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
editor.putBoolean("RequestingLU", false);
editor.apply();
Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
一些观察
如果RequestingLU设置为false,则首先出现“不连接”Toast,然后出现“您已连接”。
如果RequestingLU设置为true,则“不连接”烤面包不会显示。
交换RequestingLU out!RequestingLU不起作用,仍然有同样的效果。
我已经测试了RequestLU设置,如果我改变了它,它会返回false。该问题似乎来自应用程序没有正确检查布尔值。
当应用程序第一次启动时,尽管默认参数声明默认情况下它是true,但应该在RequestingLU为false时触发的AlertDialog。
将默认值从true更改为false根本没有任何效果。
1)显示'buildLocationClient()'2)你在哪里存储首选项? –
1)我不确定它是否相关,但我会把它放进去,2)我只是使用SharedPreferences来存储我的首选项。 –
当然,但是在哪里?我没有在你的代码中看到它。对于我们所知的所有事情总是如此。 –