This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How can i check if boolean method is true?  RSS feed

 
Ranch Hand
Posts: 104
  • 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: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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: 104
  • 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: 104
  • 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: 24369
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: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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
 
Saloon Keeper
Posts: 20643
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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: 104
  • 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: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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: 104
  • 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: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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
Saloon Keeper
Posts: 20643
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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: 104
  • 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: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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: 104
  • 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
Saloon Keeper
Posts: 20643
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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: 104
  • 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
Saloon Keeper
Posts: 20643
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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: 8
  • 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
}
 
All of the following truths are shameless lies. But what about this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!