broadcastreceiver - Android Broadcast receiver not working on app close -
i have created broadcast receiver listen connectivity change events. receiver works when minimize app using home button. when use button exit app broadcast receiver stops listening connectivity changes. searched , found should call broadcast receiver service not working on app close. here code broadcast receiver service.
internet service
public class internetservice extends service { networkchangereceiver networkchangereceiver; public internetservice() { super(); } @override public int onstartcommand(intent intent, int flags, int startid) { intentfilter filter = new intentfilter(connectivitymanager.connectivity_action); networkchangereceiver = new networkchangereceiver(); registerreceiver(networkchangereceiver, filter); return service.start_sticky; } @nullable @override public ibinder onbind(intent intent) { return null; } }
broadcast receiver
public class networkchangereceiver extends broadcastreceiver { boolean isconnected = false; @override public void onreceive(final context context, final intent intent) { isnetworkavailable(context); } private boolean isnetworkavailable(context context) { connectivitymanager connectivity = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (connectivity != null) { networkinfo info = connectivity.getactivenetworkinfo(); if (info != null) { if (info.isconnected()) { if (!isconnected) { isconnected = true; toast.maketext(getbasecontext(), "connected internet", toast.length_long).show(); log.v("internet", "internet connected"); hitdataservice(); intent intent = new intent(activityform.this, myservice.class); startservice(intent); } return true; } } } isconnected = false; return false; } }
i want send data service on app close , when mobile connected internet. there other way of getting same behaviour.
i tried adding following lines in oncreatemethod() still no luck
componentname receiver = new componentname(getbasecontext(), networkchangereceiver.class); packagemanager pm = getbasecontext().getpackagemanager(); pm.setcomponentenabledsetting(receiver, packagemanager.component_enabled_state_enabled, packagemanager.dont_kill_app);
here sample code processes alarm events, after process has been terminated ddms.
you can check implementation of broadcast receiver in above sample works after app close.
hope you.
Comments
Post a Comment