android - Why the service starts on BOOT_COMPLETED but not with BATTERY_LOW? -
why service starts on boot_completed not battery_low? code:
myschedulereceiver.java
public class myschedulereceiver extends broadcastreceiver { // restart service every 30 min private static final long repeat_time = 30*1000*4;//1800000 ; @override public void onreceive(context context, intent intent) { intent service = new intent(context, service.class); context.startservice(service); }}
service.java
public class service extends service { notificationmanager mnotificationmanager; @override public ibinder onbind(intent intent) { // not used return null; } @override public void oncreate() { super.oncreate(); mnotificationmanager= (notificationmanager)getsystemservice(notification_service); checkpref(); } @override public void ondestroy() { super.ondestroy(); } private void checkpref(){ notificationcompat.builder notificationbuilder = new notificationcompat.builder( service.this); notificationbuilder.setcontenttitle("title"); notificationbuilder.setcontenttext("context"); notificationbuilder.setticker("tickertext"); notificationbuilder.setwhen(system.currenttimemillis()); notificationbuilder.setsmallicon(r.drawable.ic_stat_icon); intent notificationintent = new intent(this, service.class); pendingintent contentintent = pendingintent.getactivity(this, 0, notificationintent, 0); notificationbuilder.setcontentintent(contentintent); notificationbuilder.setdefaults(notification.default_sound | notification.default_lights | notification.default_vibrate); mnotificationmanager.notify(1, notificationbuilder.build()); } }
and manifest
<uses-permission android:name="android.permission.receive_boot_completed"> <uses-permission android:name="android.permission.battery_stats"> <receiver android:name="myschedulereceiver" > <intent-filter> <action android:name="android.intent.action.battery_low" /> </intent-filter> </receiver> <service android:name="service" > </service>
well notification want show battery low doesn't appear.. if change action boot_completed , reboot device works..why?
look @ answer : https://stackoverflow.com/a/12348778/2508174
looks need programmaticaly subcribe intents too. , looks don't need battery_stats permission btw.
hope helps
Comments
Post a Comment