Based on this tutorial, I created a widget that should show time. Java works, but the service does not work.
HelloWidget.java:
public class HelloWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Intent intent = new Intent(context, UpdateService.class); context.startService(intent); }
UpdateService.java:
public final class UpdateService extends Service { @Override public void onStart(Intent intent, int startId) { RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main); Date date = new Date(); java.text.DateFormat format = SimpleDateFormat.getTimeInstance( SimpleDateFormat.MEDIUM, Locale.getDefault()); updateViews.setTextViewText(R.id.widget_textview, "Current Time " + format.format(date)); ComponentName thisWidget = new ComponentName(this, HelloWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, updateViews); } @Override public IBinder onBind(Intent intent) {
}
Hello_widget_provider.xml has this line: android: updatePeriodMillis = "1000"
Problem: the widget shows the current time, but is not updated (there is a java-way).
My main goal is to update the widget, for example. 18:56 every day. Idk if this is a good way, but I tried changing the onUpdate method as shown below, but it has a problem with ALARM_SERVICE: ALARM_SERVICE cannot be resolved by a variable
public class HelloWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { AlarmManager alarmManager; Intent intent = new Intent(context, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 18); cal.set(Calendar.MINUTE, 56); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400, pendingIntent);
}}
Any help is appreciated.