2016-07-25 58 views
0

我目前正在尝试创建一个简单的,以便当布尔值设置为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根本没有任何效果。

+0

1)显示'buildLocationClient()'2)你在哪里存储首选项? –

+0

1)我不确定它是否相关,但我会把它放进去,2)我只是使用SharedPreferences来存储我的首选项。 –

+0

当然,但是在哪里?我没有在你的代码中看到它。对于我们所知的所有事情总是如此。 –

回答

0

变化

Boolean RequestingLU = settings.getBoolean("RequestingLU", true); 

Boolean RequestingLU = settings.getBoolean("RequestingLU", false); 

,如果没有价值已对键定义的getBoolean()函数返回第二个参数或传递的默认值(在这种情况下,“真”)(即“RequestingLU”)

+0

OP说他确实将RequestingLU更改为false,所以这不会帮助 –

+0

他将“RequestingLU”的值更改为false,但不是默认参数。 Android可能有机会无法访问“PREFS_NAME”首选项。 –

+0

不解决,它根本没有效果... –