I am writing a service application in Android. In this application, I am receiving commands from Main activity using onStartCommand event. The IF block is checking a variable. As you can see in the picture(Debug mode), the value is "start" but the IF statement doesn't work and will redirect to Else. why?
Asked
Active
Viewed 435 times
1
Kermia
- 4,081
- 12
- 59
- 105
2 Answers
5
try with .equals will help you...
String is not Primary Data type in java
Always use .equals() when you have Object and String is Object
== is only for primitive data type and == in object compare your String Refrences...not value...
if(ReceivedCommandFromUser.equals("start"))
{
}
Samir Mangroliya
- 39,219
- 16
- 116
- 133
-
Thanks. So, would you tell me that what is wrong with current solution(using `IF` and `==` operator)? – Kermia Apr 07 '12 at 08:40
-
1@Kermia, see this (shameless link) http://stackoverflow.com/questions/9870985/if-condition-does-not-work/9870998#9870998 – hmjd Apr 07 '12 at 08:42
2
When working with Strings in Java, use the .equals() method.
For example...
if(RecievedCommandFromUser.equals("start"){}
Should work.
Chris M
- 127
- 2
- 9