Hello,
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.
So I started "SERVICE" and never stop it to prevent receiver-kill,
nevertheless receiver was died.
let me know how to 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 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