Question

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..:)

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top