现一款android游戏,一个activity类,一个SurfaceView类,现需要按Home键时游戏后台运行,按HOME键会执行onpause()和onStop()两个方法,在onPause()里面将控制线程执行的变量至false,然后再调用一个方法:
 private void showNotification() {
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定义Notification的各种属性
Notification notification = new Notification(R.drawable.icon, "项目",
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT; 
notification.flags |= Notification.FLAG_NO_CLEAR; 
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000;
CharSequence contentTitle = "项目";
CharSequence contentText = "项目正在运行……";
Intent notificationIntent = new Intent(this, GameActivity.class);
notificationIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
PendingIntent contentItent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText,
contentItent);
notificationManager.notify(0, notification);
}
然后按HOME键可以正常后台运行,但是当需要游戏恢复的时候,程序却调用onCreate()方法,但游戏不是重新运行,而是所有的游戏资源全部被释放,报空指针异常,点击屏幕后游戏出现异常退出然后游戏重新运行!有人知道是怎么回事嘛?或者有更好的后台运行的方法吗?大家踊跃发言呀,谢谢!