Android Manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
MainActivity.java
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
Button bn;
String imei;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tvimei);
bn = (Button)findViewById(R.id.button);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei = telephonyManager.getDeviceId();
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText(imei);
}
});
}
}
I was trying to get imei number and display it on homepage for testing purpose but the app crashes as soon as I run it on the phone
Hi try to add permission if your device is marshmallow check the below code also