This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am trying to detect the device model and trying to do some operations based on that.
I do the code to detect the device model and manufacturer but for some reason its giving me a negative result.
Here's the code to detect the device model and manufacturer:
private Boolean isHTCone(){
//For model and manufacturer both
String HTC_MODEL_MANUFACTURER = android.os.Build.MODEL+""+android.os.Build.MANUFACTURER;
//If true then do something
Boolean isHTC = true;
//My model name
String HTCModel = "HTC6500LVW";
//For device model
String MyBuildModel = android.os.Build.MODEL;
//For manufacturer
String MyBuildManufacturer = android.os.Build.MANUFACTURER;
//Check for the condition
if(MyBuildModel==HTCModel){
Log.i("Device Model is: ", android.os.Build.MODEL);
return true;
}
else{
Log.i("Device is not HTC One");
return false;
}
}
Its always giving me a wrong value. Even when my device model is "HTC6500LVW", it's not accepting the if loop and is going to the else.
Can anyone tell me if I am doing something wrong here. I will really appreciate it.
Thanks in advance..:)
Do like this
if(MyBuildModel.equals(HTCModel)) {
// your code
}
== operator compare the reference of two string which in your case is different.
In Java, to compare the strings, you have to use the equals() method for this.
You'll have to use String.equals() to compare your strings, like this:
if (MyBuildModel.equals(HTCModel)){
Otherwise, using == you are comparing the memory adresses of the Strings, which of course are not the same.
As a side note, variable names should start with a lower case letter.
Related
I'm a new one for android developing. I retrieved a string from hosted database. I want to compare that string and set it as a text according to comparison.
if((dataList.get(position).getname()).equals("5b275526bd5600499cce09ae")) {
holder.txtTitle.setText("denim");
}
else {
holder.txtTitle.setText("shirt");
}
}
The retrieved string is "5b275526bd5600499cce09ae". According to this settext should be "denim". But it display as "shirt".
Then I tried the following one
holder.txtTitle.setText(dataList.get(position).getname());
Then it dispayed as 5b275526bd5600499cce09ae". therefore there is no issues to be seen with the retriving value. I can't figure out with what wrong with this.
Please help me to figure out what's wrong with my code as I'm a new one for Android and Java.
This question already has answers here:
How do I make my string comparison case-insensitive?
(12 answers)
Closed 6 years ago.
i perform a special action if the string equals something. However, i want to perform the same action if the string has uppercase characters. but i dont want to write so many else if statements for every case-
decrypt
Decrypt
DECRYPT
} else if (message.equals("decrypt")) {
removeencryption(context);
is there a way of removing case sensitivity?
i have already tried
} else if (message.toLowerCase().equals("whistle")) {
whistle(context);
but it is not working
Use equalsIgnoreCase method of String class:
else if (message.equalsIgnoreCase("decrypt")) {
removeencryption(context);
}
This question already has an answer here:
Android: Determine active input method from code
(1 answer)
Closed 1 year ago.
I have an android application which handles input from Standard keyboard differently from input given by Swype keyboard. Is there any way to programmatically find out which keyboard is currently being used?
Thanks!
The best way to get that is using the InputDeviceId.
int[] devicesIds = InputDevice.getDeviceIds();
for (int deviceId : devicesIds) {
//Check the device you want
InputDevice device = InputDevice.getDevice(deviceId);
//device.getName must to have virtual
//That check the keyboard type
device.getKeyboardType(); //returns an int
}
Reference:
https://developer.android.com/reference/android/view/InputDevice.html#getKeyboardType%28%29
Use Secure static inner class of Settings to get a default keyboard type.
String defaultKeyboard = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD);
Return type will be a string sequence separated by "/", so split this string wrt to slash to get a list of substrings.
The 1st element of that list will give you the package name of your default keyboard.
defaultKeyboard = defaultKeyboard.split("/")[0];
P.S - Good practice to wrap Settings.Secure calls with try catch.
I have contact like "+919672525253".Now i extract the country code like "91" from that number.Now if number is like "9672525253" and if i extract the country code then it will give me "967".So after extracting the country code how can i check that remaining number is valid mobile number for that country code or not?
EDIT
If any body know the mobile number length country wise then also i can solve this problem.like in india 10 digits.
You pretty much can't. For example in the US mobile numbers and landline numbers are indistinguishable, they have normal area codes just like landline numbers. Even if it were possible every country does it differently and it is also constantly changing as numbers run out new prefixes are added and things change and their is no pattern you could match against or database you could do a lookup against.
Take a look at libPhoneNumber (bundled in ICS) which can help validating a phone number (see PhoneNumberUtils).
There's a MobileType you can get after validation but as stated in the source and by Ben, in some region this will not work.
EDIT:
Some validation code (here we need to check the phone is a valid one assuming it's a french one):
boolean isValid = false;
PhoneNumber number = null;
try {
number = this.phoneUtil.parse(phone, "FR"); // phone is number in internationnal format "+xxxxxx"
isValid = this.phoneUtil.isValidNumber(number);
} catch (final NumberParseException e) {
// ...
}
isValid // is the phone number valid according to the library?
this.phoneUtil.getRegionCodeForNumber(number); // this gets the country code of the phone as found by the library (for example "US", "CH", "GB", ...)
This works for us but you'll need to try it to see if it suit your need.
I have a number (123456) converted to a hash key and stored in SharedPrefs using:
String correctMd5 = passwdfile.getString(PhoneFinder.PASSWORD_PREF_KEY, null);
I then retreive the number from a string:
String[] tokens = msg.getMessageBody().split(":");
String md5hash = PhoneFinder.getMd5Hash(tokens[1]);
and compare the two:
if (correctMd5 == md5hash) {
Toast.makeText(context, "Hash OK: " + md5hash, Toast.LENGTH_SHORT).show();
}
However, this check does not complete succesfully.
If I convert to strings and display them, the hashes are the same, however if I convert to bytes the 4 right most bytes are different. I assume some special character is hidden in there somewhere, how do I check and kill it?
You should probably use correctMd5.equals(md5hash) instead of the correctMd5 == md5hash.
Is it solving the problem ?
The problem appears to be:
correctMd5 == md5hash
Because a String is an Object in Java (Android) this will compare the Object, not its String value. For instance, if you have two different variables they might be in 2 different memory locations, or they might be references to the same memory location.
On the other hand, if you want to find out if the VALUES stored by the memory location are equal, you should use
correctMd5.equals(md5hash)
In your conditional, you could have two strings "1000" and "1000" but stored at different memory locations. In my conditional, it will still be true regardless of memory location, if the String values are equal.
If == is true, .equals() should be true (in most cases, if not all). But if .equals() is true, there is no guarantee that == is true.