1

Can i convert in some way this method from boolean to int? so when i will call the method instead return true or false i can return 1 or 0:

public boolean openMode() {

return Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true); 

}
Atlas91
  • 5,754
  • 17
  • 69
  • 141

3 Answers3

3
public int openMode(){
    boolean value = Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true);

    if(value){
        return 1;
    }
    else{
        return 0;
    }

}
Bono
  • 4,757
  • 6
  • 48
  • 77
1

Unlike other programming languages like C, java does not recognizes 1 or 0 as true or false ( or boolean in short). So the return type of this method has to be boolean it self, you can not return 1 or 0 unless you change the method's return type. However, if you can change the method signature , you can change the return type to int and return 1 for true and 0 for false. example :

public int openMode(){
 return (Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true))?1:0 ;
}
Jimmy
  • 2,589
  • 21
  • 31
1

A shorter form, using a "ternary operator":

public int openMode()
{
    boolean value = Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true);
    return (value == true ? 1 : 0);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115