kevan aghera kevan aghera -4 years ago 82
Java Question

Android Widget Digital clock

i am try to implement digital clock widget. when i am drag a clock widget on my hone screen at that time its take a space on home screen but when system clock update his time at time my widget clock is display as a widget ex. mobile clock time is 2:10 and i drag my widget on screen and its not display when system clock is 2:11 at that time my widget display on home screen. here is my clockwidget code.

Clockwidget.java

public class ClockWidget extends AppWidgetProvider {

@Override
public void onDisabled(Context context) {
super.onDisabled(context);
context.stopService(new Intent(context, UpdateService.class));
}


@Override
public void onEnabled(Context context) {
super.onEnabled(context);
// context.startService(new Intent(UpdateService.ACTION_UPDATE));
context.startService(new Intent(context, UpdateService.class));


}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// context.startService(new Intent(UpdateService.ACTION_UPDATE));
context.startService(new Intent(context, UpdateService.class));

}


public static final class UpdateService extends Service {

static final String ACTION_UPDATE = "com.example.kevan.mywidget.action.UPDATE";

private final static IntentFilter sIntentFilter;

private final static String FORMAT_12_HOURS = "h:mm";
private final static String FORMAT_24_HOURS = "kk:mm";

private String mTimeFormat;
private String mDateFormat;
private Calendar mCalendar;
private String mAM, mPM;

static {
sIntentFilter = new IntentFilter();
sIntentFilter.addAction(Intent.ACTION_TIME_TICK);
sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}


@Override
public void onCreate() {
super.onCreate();
reinit();
registerReceiver(mTimeChangedReceiver, sIntentFilter);

}


@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mTimeChangedReceiver);
}


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

if (ACTION_UPDATE.equals(intent.getAction())) {
update();
}
}

@Override
public IBinder onBind(Intent intent) {
return null;
}


private void update() {
mCalendar.setTimeInMillis(System.currentTimeMillis());
final CharSequence time = DateFormat.format(mTimeFormat, mCalendar);
final CharSequence date = DateFormat.format(mDateFormat, mCalendar);

RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
views.setTextViewText(R.id.Time, time);
views.setTextViewText(R.id.Date, date);

if (!is24HourMode(this)) {
final boolean isMorning = (mCalendar.get(Calendar.AM_PM) == 0);
views.setTextViewText(R.id.AM_PM, (isMorning ? mAM : mPM));
} else {
views.setTextViewText(R.id.AM_PM, "");
}

ComponentName widget = new ComponentName(this, ClockWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(widget, views);
}

private void reinit() {
final String[] ampm = new DateFormatSymbols().getAmPmStrings();
mDateFormat = getString(R.string.date_format);
mTimeFormat = is24HourMode(this) ? FORMAT_24_HOURS : FORMAT_12_HOURS;
mCalendar = Calendar.getInstance();
mAM = ampm[0].toLowerCase();
mPM = ampm[1].toLowerCase();
}

private static boolean is24HourMode(final Context context) {
return DateFormat.is24HourFormat(context);
}


private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();

if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {

reinit();
}

update();
}
};
}


}

Answer Source

okay. that means when system time is changed at that time your widget is updating so it may be first time updated when time is changed. so my suggestion to you is that you should update your service at first time at onCreate time after that it will update when time changed. so this way your issue may be solved. try it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download