android中可能的内存泄漏。可能使用错误的清理方法,或丢失一些东西

时间:2011-01-25 06:19:32

标签: android

我有内存泄漏。这是代码

package fourguys.testing.IntentTest;

import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.media.MediaPlayer; import android.media.AudioManager; import android.content.Context;

public class CanvasDrawingActivity extends Activity {

    private static final int FIRE = 0;
    private int initVolume = 0;
    private Handler handler;
    private MyCanvas v;
    private MediaPlayer mp;
    private AudioManager am;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

        // this method gets the current volume setting for music
        initVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);


        am.setStreamVolume(AudioManager.STREAM_MUSIC,100,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

        mp = MediaPlayer.create(this, R.raw.test);

        makeHandler();
        v =new MyCanvas(this);
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true)
                handler.sendEmptyMessage(FIRE);
        }}).start();
        setContentView(v);
        mp.setLooping(true);
        mp.start();
    }
    private void makeHandler()
    {
        handler  = new Handler(){

            @Override
            public void handleMessage(Message msg) {
                switch(msg.what)
                {
                    case FIRE:
                    {
                        v.invalidate();
                        break;
                    }
                }
            }

        };
    }
    protected void onPause() {
        super.onPause();
        mp.stop();
    }
    protected void onFinish() {
        mp.stop();
    }

}

和此:

package fourguys.testing.IntentTest;

import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.view.WindowManager;

public class IntentTest extends Activity { /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //reciever intentReceiver = new reciever();

        // IntentFilter intentFilter = new IntentFilter("com.app.REC");

        //registerReceiver(intentReceiver, intentFilter);
        Button b = (Button)this.findViewById(R.id.endButton);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(IntentTest.this,CanvasDrawingActivity.class);
                startActivity(i);

            }
        });
    }
    // the onPause method get called when the app is either being hidden or being closed so this the place where we would want to clean anything up like stoping the media player.
    @Override
    protected void onPause()
    {
        super.onPause();
    }
}

我运行应用程序,退出时会变得很糟糕。它会锁定听筒并导致电池发热。我需要物理拉电池才能重启。有什么想法可能是什么?它在模拟器上运行得非常漂亮。我应该使用onFinish,还是我没有清理过什么东西而且我错过了它?

1 个答案:

答案 0 :(得分:1)

这是您的代码的一部分:

 new Thread(new Runnable(){
            @Override
            public void run() {
                while(true)
                handler.sendEmptyMessage(FIRE);
        }}).start();

你在这里犯了三个显而易见的错误。 1)你没有在Activity#onPause中杀死它和/或暂停它。 2)你没有打电话给setDaemon(true);这将导致进程继续运行而不会在此线程运行时死亡。 3)你正在使用热循环,即你没有调用Thread#sleep()或其他类型的等效方法来暂停和完全停止使用cpu。