Broadcast Receiver service in a widget
2013-05-23 06:49:50 GMT
I have a simple widget that does some calculations once the screen comes on and displays them and clears all the fields once the screen goes off ... i have a broadcast receiver setup in my service which listens to ACTION_SCREEN_ON and ACTION_SCREEN_OFF.
This works perfectly as long as the phone doesn't go to sleep for a long period of time or there is heavy usage of the phone - once this happens my widget process is killed (the service is still running but the process is killed) after this when the screen goes off and comes back on my widget doesn't update as the ACTION_SCREEN_ON intent is not caught by my service :(
public class CDTservice extends Service { <at> Override public void onCreate() { IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); m_receiver = new ScreenBroadcastReceiver(); registerReceiver(m_receiver, filter); Log.d("Widgettool", "works"); } <at> Override public int onStartCommand(Intent intent, int flags, int startId) { start(); Toast.makeText(getBaseContext(), "SERVICE ON", Toast.LENGTH_SHORT).show(); return START_REDELIVER_INTENT; } <at> Override public void onDestroy() { stop(); unregisterReceiver(m_receiver); } public void start() { RemoteViews View = new RemoteViews(this.getPackageName(), R.layout.main); updatewidgetclass x= new updatewidgetclass(this, View, widgetId); x.start(); // does calculations and displays on widget } public void stop() { RemoteViews Viewclear = new RemoteViews(this.getPackageName(), R.layout.main); updatewidgetclass y = new updatewidgetclass(this, Viewclear, widgetId); y.stop(); // clears resources and stops } private class ScreenBroadcastReceiver extends BroadcastReceiver { <at> Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { Log.d("ON SCREEN ON", "might hang here"); start(); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { stop(); } } }sometimes when the widget process is not claimed by the android system the widget works perfectly for days and perfectly displays the values ACTION_SCREEN_ON
the problem arises when - i check in settings>apps>running - i can see my widget name and it says 0 processes and 1 service
i assume the broadcast receive is happening on the main process and hence its not receiving it when the process gets killed.
I have a work around in place for this but would really like to fix the issue. Any helps is highly appreciated
img515.imageshack.us/img515/5281/screenshot2013052311325.png Screenshot of running service |
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers <at> googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe <at> googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
RSS Feed