Battle Shock

Check out Luna Sea Games to see the iPhone version of Battle Shock in the AppStore now.

Wednesday, December 8, 2010

Die app, die!

I think it really runs counter to the Android philosophy to actually kill your application when you leave it. That makes sense for email or web or times when you are jumping around to different lightweight tasks. However, a game is another animal. It consumes most of the resources of your phone. It's a high intensity activity and you are not doing email or mutli-tasking while you are playing. And when you are done, you're done! It's on the shelf. You will come back when you have time.

In my case, there was actually a very nasty bug that occurred when you left the app and came back. Somehow I managed to completely sieze up the phone making even the volume and power keys unresponsive. The only way out was to yank the battery! That's a hard lock. But it was actually just a pegged cpu. I could watch the usage spike on the task manager. I know it's my bug, but it's hard to believe that the os would allow a process to runaway like that.

Anyway, long story short, it was better for everyone if my app just quits when the users leaves. Accomplishing that took some research. Many people posted answers that didn't work for me. The one that worked was:

protected void onStop()
{

     super.onStop();


    //kill this process. It does not reload cleanly. And I think most users appreciate the app leaving memory.
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = getPackageName();
        am.restartPackage(packageName);

}


and then this would crash and throw up a dialog that my app had crashed and was ok to force quit. So to avoid that dumb message you have to add some permissions to your manifest so people know you can kill your own process.. not thrilled with the whole affair. Anyway, add this:


<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

to your manifest, and you will have an app that behaves like 99% of the other os's out there.

No comments:

Post a Comment