android - Custom service class with a ThreadPoolExecutor killed when app is off -
i need execute multipe tasks in parallel inside custom service these working :
- location service , activity recognition api.
- geofence api , rest api calls.
i'm new threads in java , android, , found best way implement use threadpoolexecutor instead of making own thread classes , dealing handler looper stuff.
when execute app, service starts, location updates , activity updates works fine inside thread. but, when close app, service restarts (when return start_sticky;
) , thread not working anymore.when (return start_not_sticky;
), service disappears. (in case, can't use startforeground())
i'm using library(smart-location-lib) location , activity updates.
- here's custom service code :
public class locationservice extends service { private threadpoolexecutor mdecodethreadpool; private blockingqueue<runnable> mdecodeworkqueue; private int number_of_cores = runtime.getruntime().availableprocessors(); private final int keep_alive_time = 1; private final timeunit keep_alive_time_unit = timeunit.seconds; public locationservice () { } @override public void oncreate() { super.oncreate(); toast.maketext(this, "location services created", toast.length_short).show(); mdecodeworkqueue = new linkedblockingqueue<runnable>(); mdecodethreadpool = new threadpoolexecutor( number_of_cores * 2, // initial pool size number_of_cores * 2, // max pool size keep_alive_time, keep_alive_time_unit, mdecodeworkqueue); } @override public int onstartcommand(intent intent, int flags, int startid) { toast.maketext(this, "location services started", toast.length_short).show(); mdecodethreadpool.execute(new locationrunnable(getapplicationcontext())); return start_sticky; } @override public void onlowmemory() { super.onlowmemory(); log.v("low memory", "|||||||||||||||||||||||||||||||||||||"); } @override public ibinder onbind(intent intent) { throw new unsupportedoperationexception("not yet implemented"); } @override public void ondestroy() { toast.maketext(this, "location services stopped", toast.length_long).show(); mdecodethreadpool.shutdown(); mdecodethreadpool.shutdownnow(); super.ondestroy(); } }
- here's runnable class code :
public class locationrunnable implements runnable, onlocationupdatedlistener, onactivityupdatedlistener { smartlocation smartlocation; public locationrunnable(context ctx) { smartlocation = new smartlocation.builder(ctx).logging(true).build(); } @override public void run() { log.v("thread", "thread started"); startlocation(); } private void startlocation() { smartlocation.location().start(this); smartlocation.activity().start(this); } @override public void onactivityupdated(detectedactivity detectedactivity) { if (detectedactivity != null) { log.v("activity", "activity updated"); } else { log.v("activity", "null"); } } int = 0; @override public void onlocationupdated(location location) { log.v("location", "location updated" + i++); } private string getnamefromtype(detectedactivity activitytype) { switch (activitytype.gettype()) { case detectedactivity.in_vehicle: return "in_vehicle"; case detectedactivity.on_bicycle: return "on_bicycle"; case detectedactivity.on_foot: return "on_foot"; case detectedactivity.still: return "still"; case detectedactivity.tilting: return "tilting"; default: return "unknown"; } } }
i'm not sure if right or best way need.
any appreciated !
i realize question old, might of others.
i think due fact code runnable.run() exits immediately, thereby ending parent thread, changes in location no longer have object posted to.
smartlocation.location().start(this); // <-- runnable
and reason update until restart might due garbage collection not clearing no longer used runnable object or existing reference within code.
Comments
Post a Comment