Detect Android app version

How can I detect the versionCode and versionName in my Android app during runtime?