Slide 10
Slide 10 text
@auditty
+AudreyTroutt
import java.lang.reflect.Method;
private void logPropertyValueForObject(Object t, String accessorMethodName) {
try {
Method m = t.getClass().getDeclaredMethod(accessorMethodName); // (1)
m.setAccessible(true); // (2) just in case this is private/not accessible
Object o = m.invoke(t); // (3) using reflection to invoke an arbitrary method
Log.d("META", accessorMethodName + " value is " + o);
} catch (Exception e) {
Log.e("META", "uh oh! Something went wrong!", e);
}
}
protected void onStop() {
super.onStop();
logPropertyValueForObject(this, "timeOnScreen"); // (4)
}
private long timeOnScreen() {
return new Date().getTime() - activityStartTime;
}
Reflection