Android How to programmatically close an app by pressing back button properly.

Everyone who uses apps knows that they are sometimes rather tough to exit. It’s not always because developers don’t care too much about it. Sometimes the api is tricky. If you want the user to exit your application by pressing the back button you need to override the onKeyDown method in your activity. The default implementation would be moveTaskToBack(true), which basically pauses your app and sends it to the background, right? Wrong! Because it only pauses the ui-thread.

Which means any other asynchronous task running keeps running. It wastes resources, leading to a worse user experience and and eventually to uninstalls. Why? Because it’s a unexpected behaviour. I mean don’t get me wrong, it’s perfectly fine if the app goes to the background because the user is getting interrupted by other apps. However if he wants to exit, he should have a possibility to do so. But what then?

Calling finish() often doesn’t do the trick. In that case you can use killProcess with the app process id.

Done. Just make sure that you have finished all your business before actually killing your app like saving preferences, etc. It will immediatelly close the app.

See also activitys-threads-memory-leaks

How To Get Android OpenGL 1.0 and 2.0 Information and Constrains – Revisited

Some time ago I’ve made a short post (here) about the basics of how to get opengl infos on an android device. For an app I’m currently working on it was required to get all interesting information once more in my android device information application. The basic idea is to add a respective opengl 1.1 or opengl 2.0 and possibly even opengl 3.0+ context to the active view, load all information in the onSurfaceCreate method of the renderer and remove the view afterwards again.

You’d use it appropriatelly like that in your activity:

Here is what I came up with:

Enjoy. (:

Java Enums in C#?

I really love C#! Almost all features like delegates, extensions methods, constraints, lambdas, refs, “magic methods”, etc. are really super convinient to use. It’s totally my favorite language so far and trust me I’ve been around with C/C++, Php, Javascript, Java. I pretty much code all the time in C# nowedays.

Probably one feature that Java did way better than C# is the way to extend enums. As stated on this website especially made for java to c# converting people.

Enums are used to create and group together a list of user defined named constants. Although on the surface the enumerated types in C# and Java seem quite similar there are some significant differences in the implementation of enumerated types in both languages. In Java, enumerated types are a full fledged class which means they are typesafe and can be extended by adding methods, fields or even implementing interfaces. Whereas in C#, an enumerated type is simply syntactic sugar around an integral type (typically an int) meaning they cannot be extended and are not typesafe.

You can use enums in Java as collection of concrecte behaviour implementations:

Or have a concrecte amount of certain Objects of a kind like a represenation of different resolutions.

Sure you can do it with normal classes, readonly attributes in c# too. But i was wondering how much of the functionality you could “port” towards c# with the help of generics, extension methods and injected attributes. So this is a small test i came up with the help of some respective stackoverflow articles.


Well it’s far from elegant/readable or even practically usable. However it’s possible to a certain extend. Feel free to correct me.

Android: OpenGL 1.0 and 2.0 infos

So here I was again, trying to figure out a way to get opengl infos like max texture size, max uniform vectors, max vertex attributes, etc. for several android devices. Technically you could just use a small getter:

The problem was that on many devices the the returned number would always be 0. That’s because there hasn’t been created an opengl context yet. As suggested on Stackoverflow you could create a GLSurfaceView, add a renderer which could read the required infos by adding the glsurfaceview to the actual main active view and simply remove itself when done. In order to get opengles 1.0 and opengles 2.0 infos you would need to create two surfacesviews with different opengl versions. The following example looks a bit complicated, because I use fragments which i need to update on the main thread after I got all the infos I wanted. A full example is on github.

How to get the MAC address on Android

(A test project can be found on github)

Since you can easily get confused with the many permissions provided by google, let me show you two ways to get the mac address on android. The first approach would be to use the WifiManager.getMacAdress().

However there are Disadvantages:

  • Device should have Wi-Fi (where not all devices have Wi-Fi)
  • If Wi-Fi present in device should be turned on otherwise does not report the MAC address
  • And the suspicious permission ACCESS_WIFI_STATE

Another approach is the use the NetworkInterface.getHardwareAddress()

Disadvantage: uses permissions too, although they are required for your app to access the internet anyway and more likely commonly known to the end-user.


Carring about permissions can be a pain in the butt there are even other ways if you just want identifying app installations, too.