Created
February 27, 2013 11:51
-
-
Save abeluck/5047372 to your computer and use it in GitHub Desktop.
Don't lose UID when launching activity with 'am start' on Android 4.2+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* As per https://code.google.com/p/android/issues/detail?id=39801 | |
* on Android 4.2 (+ ?) we need to pass the serial number to "am start" | |
* in order for the uid not to get lost. | |
* in your 'am start' line pass --user SERIAL_NUM | |
*/ | |
private static String getSerialNumber() { | |
/* We really want to use this code, but it requires bumping up the SDK to 17 so for now | |
we will use reflection. See https://bugzilla.mozilla.org/show_bug.cgi?id=811763#c11 | |
if (Build.VERSION.SDK_INT >= 17) { | |
android.os.UserManager um = (android.os.UserManager)context.getSystemService(Context.USER_SERVICE); | |
if (um != null) { | |
return um.getSerialNumberForUser(android.os.Process.myUserHandle()); | |
} else { | |
Log.d(TAG, "Unable to obtain user manager service on a device with SDK version " + Build.VERSION.SDK_INT); | |
} | |
} | |
*/ | |
try { | |
Object userManager = context.getSystemService("user"); | |
if (userManager != null) { | |
Log.d(TAG, "got us a nonnull user manager!"); | |
// if userManager is non-null that means we're running on 4.2+ | |
// and so the rest of this | |
// should just work | |
Object userHandle = android.os.Process.class.getMethod( | |
"myUserHandle", (Class[]) null).invoke(null); | |
Object userSerial = userManager | |
.getClass() | |
.getMethod("getSerialNumberForUser", | |
userHandle.getClass()) | |
.invoke(userManager, userHandle); | |
return userSerial.toString(); | |
} else | |
Log.d(TAG, "NOPE NOPE NOPE!"); | |
} catch (Exception e) { | |
// Guard against any unexpected failures | |
Log.d(TAG, "Unable to set the user serial number", e); | |
} | |
return new String(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment