Possible memory leak in android. Might be using the wrong cleanup method, or missing something -
i have memory leak. here's code
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); = (audiomanager)this.getsystemservice(context.audio_service); // method gets current volume setting 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(); } }
and this:
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 activity 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 = new intent(intenttest.this,canvasdrawingactivity.class); startactivity(i); } }); } // onpause method called when app either being hidden or being closed place want clean stoping media player. @override protected void onpause() { super.onpause(); } }
i run app , gets wonky on exit. locks handset , causes battery run hot. need pull battery physically reboot. thoughts why might be? runs fantastically on emulator. should using onfinish instead, or not cleaning , i'm missing it?
it part of code:
new thread(new runnable(){ @override public void run() { while(true) handler.sendemptymessage(fire); }}).start();
you're doing 3 obvious things wrong here. 1) you're not killing and/or pausing in activity#onpause
. 2) you're not calling setdaemon(true)
; cause process continue , not die while thread running. 3) you're using hot loop, i.e., you're not calling thread#sleep()
or other type of equivalent method there pause , stop using cpu.
Comments
Post a Comment