This is what can provide you an insight of the OS you are working on, and not about developing an app that would run on any device, as you have not made OS's for them.
Just pull a branch of android, customize it according to your wish and in your manifest remove the filters, permissions that don't allow you to do that, or add your own. Now, you have to play with the battery if I talk of more depth.
But if you just want to trigger the click on power off then it's far simpler then.
Add in your manifest:
Now a broadcast receiver for Action SHUT DOWN will work for you.
http://developer.android.com/reference/android/content/Intent.html#ACTION_SHUTDOWN