-1

I want a code to get IMEI and show in textviwe

This is my code:

public class Main extends Activity {

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

    String IMEI;
    IMEI = retrieve_IMEI.get_dev_id().toString();
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(IMEI);

}



public static class retrieve_IMEI {

    public static String get_dev_id() {

        Context ctx = null;
        // Getting the Object of TelephonyManager
        TelephonyManager tManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        // Getting IMEI Number of Device
        String Imei = tManager.getDeviceId();

        return Imei;
      }
  }

And i use READ_PHONE_STATE permission

When i run my code, i get the force close. How can i fixed

saber
  • 99
  • 1
  • 7
  • You get a force close, so you have an error. Posting the log from such error will help you get help on this matter. – Joel Jul 09 '14 at 11:31

2 Answers2

1

your context ctx is null and you are accessing TelephonyManager class with it.Please initialize it properly

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
0
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String IMEI;
    IMEI = retrieve_IMEI.get_dev_id(this).toString();
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(IMEI);

}



public static class retrieve_IMEI {

    public static String get_dev_id(Context ctx) {

        // Getting the Object of TelephonyManager
        TelephonyManager tManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        // Getting IMEI Number of Device
        String Imei = tManager.getDeviceId();

        return Imei;
      }
  }
MengMeng
  • 1,016
  • 6
  • 11