This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How can i check if boolean method is true?

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i got this boolean method from the internet that should return true if the phone is locked.
But how can i check with  an if  statement if this is true or not?
I tried  this without success    


 
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:... I tried  this without success


Can you provide details on the what was unsuccessful?

Did you include a reference to the context?
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Daniel Andersson wrote:... I tried  this without success


Can you provide details on the what was unsuccessful?

Did you include a reference to the context?


I don't understand the context part, this boolean method is declared in the MainActivity.java file.

What should i write within the () ?
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When only coding

my IDE tells me "isDeviceLocked(Context) in MainActivity cannot be applied to ()"
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You have to pass a Context object to that method. If you don't have a Context object available when you call the method then you need to revise your design. Either get a Context method from somewhere, or call some other method which doesn't need one.

Daniel Andersson wrote:So i got this boolean method from the internet



When you get code from the internet, there's a built-in assumption that understanding the code is your problem. Now most likely there's nobody to ask at the site where you got it from, but as it happens the Ranch is set up to be a place to ask about code. So ask away... but ask about the code where you have the problem. It sounds to me like the problem is in some code where you want to call the isDeviceLocked message.
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:What should i write within the () ?


MainActivity extends Activity which eventually extends Context so you should be able to use this
== true is not needed
 
Bartender
Posts: 20934
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Daniel Andersson wrote:What should i write within the () ?


MainActivity extends Activity which eventually extends Context so you should be able to use this
== true is not needed



Or desirable.
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Daniel Andersson wrote:What should i write within the () ?


MainActivity extends Activity which eventually extends Context so you should be able to use this
== true is not needed


Thanks but that did not work. I want to check if the phone is locked within a startTimer class so my IDE now tells me this:
""isDeviceLocked(Context) in MainActivity cannot be applied to (anonymous android.os.CountDownTimer)"
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:Thanks but that did not work. I want to check if the phone is locked within a startTimer class


I understood you were calling this code from the MainActivity class.  If that is not the case, then post the code for the class that you are calling it from.
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Daniel Andersson wrote:Thanks but that did not work. I want to check if the phone is locked within a startTimer class


I understood you were calling this code from the MainActivity class.  If that is not the case, then post the code for the class that you are calling it from.


Now it tells me:   """isDeviceLocked(Context) in MainActivity cannot be applied to (void)"  :/
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:... post the code for the class that you are calling it from.

 
Tim Holloway
Bartender
Posts: 20934
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"startTimer" is manifestly NOT an Android class - it doesn't even conform to standard class naming conventions.

We need to know what CLASS your method is located in - or more specifically, what Android API class is the base class for the code that won't compile.
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Ron McLeod wrote:... post the code for the class that you are calling it from.


 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class are you calling the method from?

What you have posted is a method in the CountDownTimer class.  Is CountDownTimer an anonymous class inside MainActivity, or is it in its own class?
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that CountDownTimer is an anonymous class inside MainActivity, MainActivity.this would provide a reference to the application's context
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Assuming that CountDownTimer is an anonymous class inside MainActivity, MainActivity.this would provide a reference to the application's context


Thank you sir, that code did actually work.
So I guess it wanted a class defined as "context" to get it to work?
 
Tim Holloway
Bartender
Posts: 20934
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you need to learn the precise meanings of the words that are used to describe Java code.  This took longer than it needed to because we kept asking for the name of the class that your method was in, and you were giving us everything but.

Even at the end, you said "So I guess it wanted a class defined as "context" to get it to work?". No, it wanted an object of class Context to work. "context" is not a class.
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:It looks like you need to learn the precise meanings of the words that are used to describe Java code.  This took longer than it needed to because we kept asking for the name of the class that your method was in, and you were giving us everything but.

Even at the end, you said "So I guess it wanted a class defined as "context" to get it to work?". No, it wanted an object of class Context to work. "context" is not a class.


Only in post did i miss that he asked for the class mr grumpy. But yes i have no idea what context means in java so that's why i asked, iI do need to to do some reading
 
Tim Holloway
Bartender
Posts: 20934
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I come off as surly, but we did ask "what is the class?" multiple times.

"context" in Java means different things, depending on the, um, context. But the difference between a class and an object (class instance) and an isolated method is very important.

We didn't have enough, er, context to determine what you needed.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's a method, to call it you should use parens afterwards, so your code would then become:

if(isValid()) {
   // something
} else {
   //something else
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!