2

有没有办法强制清除应用程序中所有使用的资源?我想在 onDestroy 函数中清理缓存,但我真的不知道这是否足够。

我在关闭应用程序后无法释放资源,因为我在应用程序中打开了很多图像、声音等。如果我第二次快速打开应用程序,很多时候它无法打开某些资源,因为它仍然处于打开状态......

请问有什么提示吗?谢谢

4

2 回答 2

1

位图

  • 如果您通过 加载它们BitmapFactory,它们将缓存在内存中,并且不应阻止您再次打开它。但是,请调用您创建recycle()的对象。Bitmap
  • 如果您使用另一个 Activity 打开它们,则无需担心任何事情。

声音和视频

  • 如果您使用 来打开它们,请在完成时MediaPlayer调用stop()和。release()MediaPlayer
  • 如果您使用另一个 Activity 打开它们,则无需担心任何事情。

这些只是我的(有限)意见,在继续之前也请查看其他答案。

于 2013-11-05T19:35:37.113 回答
0

一旦活动/应用程序完成,我在释放内存时遇到了完全相同的问题。所以我使用清理方法!
在每个活动的 onDestroy 中,都会调用方法 clean up。

 protected void onDestroy(){
      cleanUp(findViewById(android.R.id.content));
    }

 public void Cleanup(View rootView) {
        unbindDrawables(rootView);
        System.gc();
    }

  private void unbindDrawables(View view) {
        if (view.getBackground() != null)
            view.getBackground().setCallback(null);

        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            imageView.setImageBitmap(null);
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                unbindDrawables(viewGroup.getChildAt(i));

            if (!(view instanceof AdapterView))
                viewGroup.removeAllViews();
        }
    }
于 2013-11-05T21:20:12.280 回答