On Wed, Mar 16, 2011 at 4:37 AM, hardrock <[email protected]> wrote:
> My digital clock is home screen widget.
> It works well but sometimes time's frozen.
>
> As I know, the receiver is no longer active after return from
> onReceive().
> This means that system will consider its process to be empty and
> aggressively kill it.

This is a good thing.

> So I started "SERVICE" and never stop it to prevent receiver-kill,
> nevertheless receiver was died.

Please don't do that.

> let me know how to design longer-running widget.

You don't "design longer-running widget".

> Here is my code.
> ................................................................................
>
> public class DigitalClock extends AppWidgetProvider {
>
>    private static Context mContext;
>
>    private static final BroadcastReceiver mIntentReceiver = new
> BroadcastReceiver() {
>        @Override
>        public void onReceive(Context context, Intent intent) {
>
>            mContext = context;
>
>            // Post a runnable to avoid blocking the broadcast.
>            mHandler.post(new Runnable() {
>                public void run() {
>
> if( mIntent.getAction().equals(Intent.ACTION_TIME_TICK) ) {
>                        // start service for update time
>                        Intent intent = new Intent(mContext,
> UpdateService.class);
>                        mContext.startService(intent);
>                    }
>                }
>            }
>        }
>    }
>
>    @Override
>    public void onEnabled(Context context) {
>        super.onEnabled(context);
>
>        // register receiver action
>        IntentFilter filter = new IntentFilter();
>        filter.addAction(Intent.ACTION_TIME_TICK);
>
> context.getApplicationContext().registerReceiver(mIntentReceiver,
> filter);
>    }
>
>    public static class UpdateService extends Service {
>        @Override
>        public int onStartCommand(Intent intent, int flags, int
> startId) {
>            // update time
>            RemoteViews remoteViews = new
> RemoteViews(getPackageName(), R.layout.main);
>            String strFormat = String.format("%02d:%02d", mHour,
> mMinute);
>            remoteViews.setTextViewText(R.id.TextView01, strFormat);
>
>            ComponentName watchWidget = new ComponentName(this,
> DigitalClock.class);
>            AppWidgetManager manager =
> AppWidgetManager.getInstance(this);
>            manager.updateAppWidget(watchWidget, remoteViews);
>
>            return START_STICKY;
>        }
>    }
>
> }
>

You do NOT create Handlers in BroadcastReceivers, because it doesn't
work, as you discovered.

You do NOT create app widgets that update every second, because it is
terribly inefficient in terms of battery life and RAM.

If you want a digital clock on your home screen (besides the one that
may be provided by your device), write your own custom home screen.

If you want to create a sample app widget, do not create a digital
clock (or, at best, create one that updates every minute, using
AlarmManager).

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 3.5 Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to