现一款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()方法,但游戏不是重新运行,而是所有的游戏资源全部被释放,报空指针异常,点击屏幕后游戏出现异常退出然后游戏重新运行!有人知道是怎么回事嘛?或者有更好的后台运行的方法吗?大家踊跃发言呀,谢谢!

解决方案 »

  1.   

    application的属性中设置 clearTaskOnLaunche="true"
      

  2.   

    onCreate的参数你看到了吧,Bundle savedInstanceState 这个你可以处理具体的,查看 onSaveInstanceState和onRestoreInstanceState的用处 
      

  3.   

    都不管用,还有其他的方法吗?或者其他的HOME键的处理后台运行!
      

  4.   

    http://blog.csdn.net/yiyaaixuexi/article/details/6604430
      

  5.   

    你的activity 在转入后台后,有可能已经被GC回收了,所以,再一次回来的时候走的是onCreate(),导致了空指针异常。
      

  6.   

    怎么才能不被gc回收,再一次回来的时候不走oncreate呢?