Last active
April 13, 2016 08:16
-
-
Save rocboronat/634c1eb1ad10509cd305 to your computer and use it in GitHub Desktop.
Calling setTaskDescription() in a reflected way
This file contains hidden or 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
private void setTaskDescription(){ | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
String title = getString(R.string.app_name); | |
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); | |
int color = getResources().getColor(R.color.task_background); | |
// The legacy method call | |
// setTaskDescription(new ActivityManager.TaskDescription(title, icon, color)); | |
// The reflected method call | |
try { | |
Class<?> clazz = Class.forName("android.app.ActivityManager$TaskDescription"); | |
Constructor<?> cons = clazz.getConstructor(String.class, Bitmap.class, int.class); | |
Object taskDescription = cons.newInstance(title, icon, color); | |
Method method = ((Object) BaseActivity.this).getClass().getMethod("setTaskDescription", clazz); | |
method.invoke(this, taskDescription); //this is your Activity instance | |
} catch (Exception e) { | |
// Log this :) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good