Android widget update

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) { // TODO Auto-generated method stub return null; } 

}

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.

+6
android widget
source share
2 answers

Looking back when I asked this question, I understand how much I learned over the past 1.5 years.

Since I just worked with widgets in the last two weeks, and I just found this unanswered question, I decided to solve it. I hope this helps others.

AppWidgetProvider should look like this:

 public class MyWidget extends AppWidgetProvider { public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget"; public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; private PendingIntent service = null; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); final Calendar TIME = Calendar.getInstance(); TIME.set(Calendar.MINUTE, 0); TIME.set(Calendar.SECOND, 0); TIME.set(Calendar.MILLISECOND, 0); final Intent i = new Intent(context, MyService.class); if (service == null) { service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); } m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 1, service); } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); } @Override public void onDisabled(Context context) { super.onDisabled(context); final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if (service != null) { m.cancel(service); } } @Override public void onEnabled(Context context) { super.onEnabled(context); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); } } 

Service:

 public class MyService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { buildUpdate(); return super.onStartCommand(intent, flags, startId); } private void buildUpdate() { String lastUpdated = DateFormat.format("hh:mm:ss", new Date()).toString(); RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_layout); view.setTextViewText(R.id.btn_widget, lastUpdated); ComponentName thisWidget = new ComponentName(this, MyWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, view); } @Override public IBinder onBind(Intent intent) { return null; } } 

Also register the provider and service in the manifest:

 <receiver android:name="MyWidget" android:label="Tutorial_Widget 1x1"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="com.something.MyWidget.ACTION_WIDGET_RECEIVER"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/> </receiver> <service android:enabled="true" android:name=".MyService" /> 
+15
source share

ALARM_SERVICE is a static field of the Context class. So enter Context.ALARM_SERVICE or use static imports.

Remember that when you set the value of the updatePeriodMillis attribute, it is not guaranteed so that your onUpdate method is called with this particular period.

+3
source share

All Articles