4

I want to compare IMEI number in my phone with the data I declare, but the bug says: 'Operator == can not be applied to 'java.lang.String', 'long' How do I fix that?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TelephonyManager manager;
    private Button button;
    TextView textView;
    String IMEI;
    long IMEI2 = 356261058647361;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.next);
        textView = (TextView)findViewById(R.id.textview1);

        manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        IMEI = manager.getDeviceId();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(IMEI == IMEI2){
                    textView.setText("IMEI NUMBER : " + IMEI);

                }else{
                    textView.setText("data IMEI and IMEI2 did not match");
                }

            }
        });
    }

}
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
borneo
  • 97
  • 9

4 Answers4

3

Try this

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(IMEI2.equals(""+IMEI){
                    textView.setText("IMEI NUMBER : " + IMEI);

                }else{
                    textView.setText("data IMEI and IMEI2 did not match");
                }

            }
        });
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
3
if(IMEI.equals(String.valueOf(IMEI2)){
    textView.setText("IMEI NUMBER : " + IMEI);
}else{
    textView.setText("data IMEI and IMEI2 did not match");
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
3

Use Datatype Long instead of long. Always use L at the end of your value while initiating Long/long datatype and check it using equals

Long IMEI;
Long IMEI2 = 356261058647361L;

IMEI = Long.valueOf(manager.getDeviceId());

if (IMEI2.equals(IMEI)) {
    textView.setText("IMEI NUMBER : " + IMEI);
} else {
    textView.setText("data IMEI and IMEI2 did not match");
}
Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
2

In java for long you need to declare like long IMEI2 = 356261058647361L;

you need to add 'L' . refere : Long type

Long.parseLong()

The Long.parseLong() static method parses the string argument as a signed decimal long and returns a long value.

Use :

Long.parseLong(IMEI);

Use this way : long tmp = Long.parseLong(IMEI); before the if condition.

then in if :

if (tmp == IMEI2)

Reference: Long.parseLong()

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55