I’m trying to build my first android widget – its a simple icon, on which when i click somethings hapens – on “something” I’m using service. Everything goes fine, but i have a little problem, which I don’t understand. This works fine:
CLASS MYWIDGET.JAVA
------------------------------------------------
public class MyWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) context.startService(new Intent(context, UpdateService.class));
else super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends IntentService {
public UpdateService() {
super("MyWidget$UpdateService");
}
@Override
protected void onHandleIntent(Intent arg0) {
ComponentName me = new ComponentName(this, MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(me, buildUpdate(this));
}
private RemoteViews buildUpdate (Context context) {
// i'm doing something here
}
}
}
but when i extract UpdateService to separate file it doesn’t work:
CLASS MYWIDGET.JAVA
------------------------------------------------
public class MyWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) context.startService(new Intent(context, UpdateService.class));
else super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
}
}
CLASS UPDATESERVICE.JAVA
-------------------------------
public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
}
@Override
protected void onHandleIntent(Intent arg0) {
ComponentName me = new ComponentName(this, MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(me, buildUpdate(this));
}
private RemoteViews buildUpdate (Context context) {
// i'm doing something here
}
}
}
This version doesnt work – when i click on widget icon, no method in update service is never called. Where should be the problem?
If you move or rename the service class, you will need to reflect this change in your
AndroidManifest.xml.