I'm using following code to get device Id.
public static String getDeviceUuId(Activity context) {
String UUID = "";
String android_id = Secure.getString(context.getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
final TelephonyManager tm = (TelephonyManager) context.getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) {
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
UUID = deviceUuid.toString();
return UUID;
}
return UUID;
}
It works great, but for nexus 5(OS 6.0.1) I'm getting crash. Below is my crash logs.
3-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: FATAL EXCEPTION: main
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: Process: com.my.package, PID: 13825
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: java.lang.SecurityException: getDeviceId: Neither user 10159 nor current process has android.permission.READ_PHONE_STATE.
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1620)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1573)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4207)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.my.package.utils.DeveloperHelper.getDeviceUuId(DeveloperHelper.java:26)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.my.package.model.request.Device.<init>(Device.java:22)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.my.package.model.request.LoginObject.<init>(LoginObject.java:23)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.my.package.activities.LoginActivity.onClick(LoginActivity.java:57)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.view.View.performClick(View.java:5204)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21153)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
App specifically crashed at following line.
tmDevice = "" + tm.getDeviceId();
It is complaining about permission, but it is already added and working as well on other devices.
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.package"
android:versionCode="2"
android:versionName="2.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme">
<activity
android:name="com.my.package.activities.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.my.package.activities.StarterActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.my.package.activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- FACEBOOK -->
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<meta-data
android:name="com.facebook.sdk.ApplicationName"
android:value="@string/app_name" />
<!-- FACEBOOK -->
<!-- PLAY SERVICES -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Any help will be great.
Since READ_PHONE_STATE is considered a dangerous permission in Android M (6.0) you need to request the permission at runtime, before trying to get the UUID in your Activity do the following:
declare a
private static final int REQUEST_PHONE_STATE = 1;
then before trying to get the UUID you can call this method:
private void checkForPhoneStatePermission(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
showPermissionMessage();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
REQUEST_PHONE_STATE);
}
}
}else{
//... Permission has already been granted, obtain the UUID
getDeviceUuId(MainActivity.this);
}
}else{
//... No need to request permission, obtain the UUID
getDeviceUuId(MainActivity.this);
}
}
private void showPermissionMessage(){
new AlertDialog.Builder(this)
.setTitle("Read phone state")
.setMessage("This app requires the permission to read phone state to continue")
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
REQUEST_PHONE_STATE);
}
}).create().show();
}
then add this to your MainActivity class, which handles the result from the permission request:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case REQUEST_PHONE_STATE:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// .. Can now obtain the UUID
getDeviceUuId(MainActivity.this);
}else{
Toast.makeText(MainActivity.this, "Unable to continue without granting permission", Toast.LENGTH_SHORT).show();
}
break;
}
}
For more on Android M Permission check out this
Good Luck and Happy Coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With