我正在尝试构建我的第一个 android 小部件 - 它是一个简单的图标,当我点击某些东西时,它会发生 - 在“某物”上我正在使用服务。一切都很好,但我有一个小问题,我不明白。这工作正常:
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
}
}
}
但是当我将 UpdateService 提取到单独的文件时它不起作用:
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
}
}
}
这个版本不起作用 - 当我点击小部件图标时,更新服务中的任何方法都不会被调用。问题应该出在哪里?