Skip to content

Instantly share code, notes, and snippets.

@rocboronat
Last active April 13, 2016 08:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocboronat/634c1eb1ad10509cd305 to your computer and use it in GitHub Desktop.
Save rocboronat/634c1eb1ad10509cd305 to your computer and use it in GitHub Desktop.
Calling setTaskDescription() in a reflected way
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 :)
}
}
}
@ruijun
Copy link

ruijun commented Apr 13, 2016

Good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment