android - Location update in background service not working after exit application -
i have implemented service updates location in background, android app not working(crashed) while existing application using key. following code.
public class locationservice extends service { private static final string tag = "location"; private locationmanager mlocationmanager = null; private static final int location_interval = 0; private static final float location_distance = 0f; private static context mcontext; private static int messagecount; private class locationlistener implements android.location.locationlistener{ location mlastlocation; public locationlistener(string provider) { mcontext = (context)locationdetails.locationcontext; log.e(tag, "locationlistener " + provider); mlastlocation = new location(provider); } @suppresswarnings("deprecation") @override public void onlocationchanged(location location) { try { messagecount++; log.e(tag, "onlocationchanged: " + location); mlastlocation.set(location); notificationmanager nm = (notificationmanager)getsystemservice(context.notification_service); notification notification = new notification(r.drawable.ic_launcher, "text", system.currenttimemillis()); intent intenttl = new intent(mcontext, locationdetails.class); notification.setlatesteventinfo(mcontext, "title", string.valueof(location.getlatitude()), pendingintent.getactivity(mcontext, 0, intenttl, pendingintent.flag_cancel_current)); notification.flags |= notification.flag_auto_cancel; notification.defaults |= notification.default_sound; notification.defaults |= notification.default_vibrate; notification.number += messagecount; nm.notify(messagecount, notification); } catch(exception error) { system.out.println(error.getmessage()); } } @override public void onproviderdisabled(string provider) { log.e(tag, "onproviderdisabled: " + provider); } @override public void onproviderenabled(string provider) { log.e(tag, "onproviderenabled: " + provider); } @override public void onstatuschanged(string provider, int status, bundle extras) { log.e(tag, "onstatuschanged: " + provider); } } public class servicebinder extends binder { locationservice getservice() { return locationservice.this; } } locationlistener[] mlocationlisteners = new locationlistener[] { new locationlistener(locationmanager.gps_provider), new locationlistener(locationmanager.network_provider) }; @override public ibinder onbind(intent arg0) { return null; } @override public int onstartcommand(intent intent, int flags, int startid) { log.e(tag, "onstartcommand"); super.onstartcommand(intent, flags, startid); return start_sticky; } @override public void oncreate() { mcontext = (context)locationdetails.locationcontext; log.e(tag, "oncreate"); initializelocationmanager(); try { mlocationmanager.requestlocationupdates( locationmanager.network_provider, location_interval, location_distance, mlocationlisteners[1]); } catch (java.lang.securityexception ex) { log.i(tag, "fail request location update, ignore", ex); } catch (illegalargumentexception ex) { log.d(tag, "network provider not exist, " + ex.getmessage()); } try { mlocationmanager.requestlocationupdates( locationmanager.gps_provider, location_interval, location_distance, mlocationlisteners[0]); } catch (java.lang.securityexception ex) { log.i(tag, "fail request location update, ignore", ex); } catch (illegalargumentexception ex) { log.d(tag, "gps provider not exist " + ex.getmessage()); } } @override public void ondestroy() { log.e(tag, "ondestroy"); super.ondestroy(); if (mlocationmanager != null) { (int = 0; < mlocationlisteners.length; i++) { try { mlocationmanager.removeupdates(mlocationlisteners[i]); } catch (exception ex) { log.i(tag, "fail remove location listners, ignore", ex); } } } } private void initializelocationmanager() { log.e(tag, "initializelocationmanager"); if (mlocationmanager == null) { mlocationmanager = (locationmanager) getapplicationcontext().getsystemservice(context.location_service); } } }
i used following code start locationservice.
intent locationservice = new intent(); locationservice.setclass(context, locationservice.class); context.startservice(locationservice);
while exiting app using key, service not called, not getting notification. how fix issue?
i register service inside tag below.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <activity android:name=".locationdetails" android:configchanges="orientation|keyboardhidden|screensize" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name=".locationservice" /> </application>
try this..
intent locationservice = new intent(); locationservice.setclass(context, locationservice.class); context.startservice(locationservice); finish();
once service created finish app exit.
Comments
Post a Comment