In the Android game I am working on, the game is killed by android while in the background. The game is built using AndEngine, so it takes place all within a single OpenGL activity, which moves between menus and the game using scenes. I am looking to keep the game alive if it is moved to the background while the game scene is active (i.e., the user actually playing a game).
Optimally, I would like an ongoing notification to be displayed if the game is in-progress in the background, that the user can click to bring the game back to the foreground. (An example of this behaviour is the GBA Emulator GameBoid)
Handling pause and resume state is tricky in Android in general, and even trickier with AndEngine. You might want to start by reading about handling of pause and resume in general with android, so you get an idea of what methods are called by android when, for example, a phone call comes in or the user hits the power button. Take a look at these Q&A's:
Now for the AndEngine specific stuff. One approach (which I got from here), is to serialize the state using JSON. Here's how @plastic-surgeon put it:
The solution I found was to serialize the state of the engine as JSON and save it as shared storage. Its a lot of work, but it does save it. Since andengine is going to re-init your engine and textures after a pause, I think there are not many choices, unless you want to re-write some of andengine for your game. (which you can)
In my JSON I record the position type velocity etc of each sprite. The complexity of what gets saved depends on the complexity of your game. And then to each class I added a deserialize method that accepts the JSON as input.
Thanks to @plastic-surgeon, I discovered GSON, which as he says, is completely and totally invaluable.
Now, as to the final part of your question, "I would like an ongoing notification to be displayed if the game is in-progress in background," I assume you're talking about one of these:
Work on the notifications after you've got a basic handle on pausing and resuming state. To do a notification, you're going to want to read up on displaying notifications to the notification area. Here's some sample code to give you an idea:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
You can read more on creating and displaying notifications at Google's developer docs.