Saturday, April 30, 2011

[android-developers] Handling ACTION_SCREEN in a widget

Hi,

I'm doing a widget that needs to be update on screen_on or on
user_present. As I'm not hable to register a BroadcastReceiver inside
the widget I'm doing it in a Service that is triggered by the widget
like this:

Widget.java:
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        context.startService(new Intent(context,
WidgetService.class).setAction(WidgetService.ACTION_WIDGET_START));
    }

WidgetService.java:
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        String action = intent.getAction();
        Log.d(LOG_APP,"WidgetService onStart: "+action);


        if (ACTION_WIDGET_STOP.equals(action)) {
            this.unregisterReceiver(mReceiver);
            stopSelf();
            return;
        } else if (ACTION_WIDGET_START.equals(action)) {
            // register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            this.registerReceiver(mReceiver, filter);
            return;
        }

The problem is that when I try to register the receiver I get the
error that the service has leaked IntentReceiver because it was
already registered. Ive checked and no duplicate registration was done
in my code.

Do you have any idea what I'm overlooking?

Thanks,
PMD

--
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@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate