android - How can i register a Receiver in a service? -
i have register receiver it's not in same class. mean, have 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 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); } } now have register receiver like:
registerreceiver(//thereceiver, new intentfilter(intent.action_battery_changed)); inside oncreate. how can it? think in //thereceiver have write myschedulereceiver of course if write in oncreate inside service can't find it. how can do? thanks
try
registerreceiver(yourreceiver, new intentfilter("android.intent.action.battery_changed")); you might need add the
<uses-permission android:name="android.permission.broadcast_sticky"/> in manifest file
Comments
Post a Comment