Monday, 15 February 2010

notifications - Background service not starting android -



notifications - Background service not starting android -

i'm trying utilize background service can check new content on app every 30 minutes , notify user notification if there new content. problem that service not seem starting @ all. i'm not exclusively sure have wrong.

i've followed article implement notification service , question - trying start service on boot on android.

androidmanifest.xml

<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.receive_boot_completed" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.access_network_state" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.____.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name="com.____.bootreceiver"> <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver> <service android:name="com.____.notificationservice"/> </application>

the bootreceiver should start service

public class bootreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent startserviceintent = new intent(context, notificationservice.class); context.startservice(startserviceintent); } }

the service notificationservice right set display simple notification

public class notificationservice extends service { private wakelock mwakelock; /** * homecoming null, since our service not communicating * other components. work silently. */ @override public ibinder onbind(intent intent) { homecoming null; } /** * initialize. phone call when onstart/onstartcommand * called system. won't intent here, , * won't, either. */ private void handleintent(intent intent) { // obtain wake lock powermanager pm = (powermanager) getsystemservice(power_service); mwakelock = pm.newwakelock(powermanager.partial_wake_lock, "my app"); mwakelock.acquire(); // check global background info setting connectivitymanager cm = (connectivitymanager) getsystemservice(connectivity_service); if (cm.getactivenetworkinfo() == null) { stopself(); return; } // actual work, in separate thread new polltask().execute(); } private class polltask extends asynctask<void, void, void> { /** * work. there's nil me write here * have fill in. create http request(s) or whatever * have updates in here, because run in * separate thread */ @suppresslint("newapi") @override protected void doinbackground(void... params) { // stuff! // lastly added date time of offer // every 60 minutes check new offers notification notification = new notification.builder(getapplicationcontext()) .setcontenttext("new content available") .setsmallicon(r.drawable.ic_launcher) .setwhen(0) .build(); homecoming null; } /** * in here should interpret whatever fetched in doinbackground * , force notifications need status bar, using * notificationmanager. not cover here, go check docs on * notificationmanager. * * have phone call stopself() after you've pushed * notification(s). will: * 1) kill service doesn't waste precious resources * 2) phone call ondestroy() release wake lock, device * can go sleep 1 time again , save precious battery. */ @override protected void onpostexecute(void result) { // handle info stopself(); } } /** * deprecated, have implement if you're planning on * supporting devices api level lower 5 (android 2.0). */ @override public void onstart(intent intent, int startid) { handleintent(intent); } /** * called on 2.0+ (api level 5 or higher). returning * start_not_sticky tells scheme not restart service if * killed because of poor resource (memory/cpu) conditions. */ @override public int onstartcommand(intent intent, int flags, int startid) { handleintent(intent); homecoming start_not_sticky; } /** * in ondestroy() release our wake lock. ensures whenever * service stops (killed resources, stopself() called, etc.), wake * lock released. */ public void ondestroy() { super.ondestroy(); mwakelock.release(); } }

debugging app breakpoints in both bootrecevier , notificationservice never triggered. maybe i'm misunderstanding way background services work.

update

i found article why broadcastrecevier not beingness called. 1 of mentioned points pendingintent requestcode missing

i've updated bootrecevier follows test out service...called every 1 minute:

@override public void onreceive(context context, intent intent) { int minutes = 1; alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); intent = new intent(context, notificationservice.class); pendingintent pi = pendingintent.getservice(context, 54321, i, 0); am.cancel(pi); if (minutes > 0) { am.setinexactrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime() + minutes*60*1000, minutes*60*1000, pi); } }

changing 0 'unique' requestcode 54321 somehow started triggering service. issue service not carry on working when app closed...not sure if that's different thing altogether.

update 2

i've updated doinbackground method in notificationservice, using example display notifications:

@suppresslint("newapi") @override protected void doinbackground(void... params) { context mcontext = getapplicationcontext(); // invoke default notification service notificationcompat.builder mbuilder = new notificationcompat.builder(mcontext); mbuilder.setcontenttitle("[notification title]"); mbuilder.setcontenttext("[notification text]"); mbuilder.setticker(mcontext.getstring(r.string.app_name)); mbuilder.setsmallicon(r.drawable.ic_launcher); intent resultintent = new intent(mcontext, mainactivity.class); taskstackbuilder stackbuilder = taskstackbuilder.create(mcontext); stackbuilder.addparentstack(mainactivity.class); stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent(0, pendingintent.flag_one_shot); mbuilder.setcontentintent(resultpendingintent); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); mnotificationmanager.notify(1, mbuilder.build()); homecoming null; }

i've tested on emulator , background service works when app closed. however, testing app on actual device did not display notifications.

you need declare permission receive intent in manifest.

androidmanifest.xml

<uses-permission android:name="android.permission.receive_boot_completed" />

http://developer.android.com/reference/android/manifest.permission.html#receive_boot_completed

you receive notifications of device booting after user has started application @ to the lowest degree once.

android notifications background-service

No comments:

Post a Comment