Мой Android версии Зефир 6,0
Примечание: Вы должны Permissions среды выполнения
Я добавил разрешение READ_PHONE_STATE в Android Файл Manifest.xml.
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
в MainActivity:
private static final int PERMISSIONS_REQUEST_READ_PHONE_STATE = 999;
private TelephonyManager mTelephonyManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
PERMISSIONS_REQUEST_READ_PHONE_STATE);
} else {
getDeviceImei();
}
....
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_PHONE_STATE
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getDeviceImei();
}
}
private void getDeviceImei() {
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = mTelephonyManager.getDeviceId();
Log.d("msg", "DeviceImei " + deviceid);
}
Отметьте это сообщение: http: //stackoverflow.com/questions/33078003/android-6-0-permission-error – Rafal
какую ошибку вы получаете? – Amy