Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

Android service connection leaked after starting new activity -


i trying figure out why service leaking application.

the official error getting service not registered longer.

here's works: create service creates listener, when listener triggered service sets of intent start activity. new activity begins , thing.

the problem: when main screen gives me option turn off service, error stated causes illegalargumentexception (when try unbind service not registered).

any appreciated. here code service. i've included because seems problem is, if need more let me know.

thanks in advance, here code.

import java.lang.ref.weakreference; import java.util.list;  import android.app.service; import android.content.context; import android.content.intent; import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.os.binder; import android.os.ibinder; import android.util.log; import android.widget.toast;   public class accelservice extends service { public static boolean listening = false; public boolean callmade = false; private static sensor sensor; private static sensormanager asensormanager;   private sensoreventlistener eventlistener =      new sensoreventlistener() {      private float x = 0;     private float y = 0;     private float z = 0;     private double max = 0;     private double force = 0;      public void onaccuracychanged(sensor sensor, int accuracy) {}      public void onsensorchanged(sensorevent event)      {          x = event.values[0];         y = event.values[1];         z = event.values[2];         force = math.sqrt(x*x+y*y+z*z);         log.i("localservice", "event happened: " + force);           if (force > main.dropvalue)         {             ondrop(force);         }     }  };  public void startlistener() {     asensormanager = (sensormanager) this.getsystemservice(context.sensor_service);     list<sensor> sensors = asensormanager.getsensorlist(sensor.type_accelerometer);     if (sensors.size() > 0)      {         sensor = sensors.get(0);         listening = asensormanager.registerlistener(acceleventlistener, sensor, sensormanager.sensor_delay_game);      } }      public class accelbinder<s> extends binder  {     private weakreference<s> mservice;      public accelbinder (s service)     {         mservice = new weakreference<s>(service);     }      public s getservice()     {         return mservice.get();     } }  public ibinder mbinder;  @override public void oncreate() {     startlistener();      mbinder = new accelbinder<accelservice>(this); }  public boolean islistening() {     return listening; }  /*@override public void onstart(intent intent, int startid) {     log.i("localservice", "received start id " + startid + ": " + intent); }*/  @override public int onstartcommand(intent intent, int flags, int startid)  {     log.i("localservice", "received start id " + startid + ": " + intent);     return accelservice.start_sticky; }  @override public void ondestroy()  {     if (listening)         stoplistening();     mbinder = null;      super.ondestroy(); }  public void ondrop(double force) {     if (!callmade)     {         toast.maketext(this, "phone dropped: " + force, 5000).show();         intent = new intent(this,next.class);         i.addflags(intent.flag_activity_new_task);         callmade = true;         //stoplistening();         //ondestroy();         //safetynet.ctxt.unbindservice(safetynet.accelwatch);         this.startactivity(i);     } }  public void stoplistening() {             listening = false;     try {         if (asensormanager != null && acceleventlistener != null)          {             asensormanager.unregisterlistener(acceleventlistener);         }     } catch (exception e) {}         }  @override public ibinder onbind(intent intent)  {     return mbinder; }  } 

i don't know sensors, service looking pretty good.

if accelbinder inner class of service make static inner class or, do, separate class altogether. static inner classes don't have reference outer class. remember leak binder. if binder non-static inner class, has reference service leak well.

i guessing - wildly guessing - in absence of code there wrong management of activity life-cycle , how handle binder objects.

things keep in mind....

don't keep static reference binder object - disposable - new 1 each time bind.

keep binding symmetrical regards activity life-cycle. if bind in oncreate(), unbind in ondestroy() ( or onpause if isfinishing() ) etc. particularly important if don't understand physical rotation of phone sensor destroys activity recreates scratch.

you seem altogether fond of "static" class variables. in android static things tend lead memory leaks - , if leak has context - things nasty. if want keep state between instances of same class, consider using preferences instead.

for example,

private static sensor sensor; private static sensormanager asensormanager; 

throw these away between uses.

make sure not keeping static reference service in activity. services singletons nature of android, if still running same service each time bind. count on service being killed os @ point in time. write service it's job if restarted afresh.

that's enough guessing 1 day.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -