【发布时间】:2011-07-13 20:09:10
【问题描述】:
我有一个带有小部件的 Android 应用程序,它有按钮。 此代码有效。
当发生某些事情时,小部件上的按钮停止工作,例如更改手机的语言。我使用共享首选项,因此如果用户重新安装应用程序(没有卸载),按钮将再次起作用并且设置保持不变。
- 我注意到我的
AppWidgetProvider类(此分析下的代码)中的意图未正确触发。 - 我向从
AppWidgetProvider实例化的Call1类添加了一条Toast消息,但它没有显示。 - 我的
UpdateService.java只是获取设置的首选项并自定义小部件的外观,所以我认为这可能与我的问题无关。 - 我的
Main.java文件仅包含微调器并保存共享首选项,这意味着我在微调器中选择“计算机”,这样“计算机”文本就会出现在小部件上。当我更改手机的语言时,它也不会消失,图像也不会消失。所以,我相信UpdateService.java一定没问题。
这里是 AppWidgetProvider 类:
public class HelloWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_CONFIGURE2 = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public static String ACTION_WIDGET_RECEIVER2 = "ActionReceiverWidget";
private static final int REQUEST_CODE_FOUR = 40;
private static final int REQUEST_CODE_FIVE = 50;
private static final int REQUEST_CODE_SIX = 60;
private static final int REQUEST_CODE_SEVEN = 70;
private static final int REQUEST_CODE_EIGHT = 80;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
//Intent widgetUpdateIntent = new Intent(context, UpdateService.class);
//context.startService(widgetUpdateIntent );
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain2);
//P1 starts Call1.class
Intent configIntent4 = new Intent(context, Call1.class);
configIntent4.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent4 = PendingIntent.getActivity(context, REQUEST_CODE_FOUR, configIntent4, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView01, configPendingIntent4);
//P2 starts Call2.class
Intent configIntent5 = new Intent(context, Call2.class);
configIntent5.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent5 = PendingIntent.getActivity(context, REQUEST_CODE_FIVE, configIntent5, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView02, configPendingIntent5);
//P3 starts Call3.class
Intent configIntent6 = new Intent(context, Call3.class);
configIntent6.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent6 = PendingIntent.getActivity(context, REQUEST_CODE_SIX, configIntent6, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView03, configPendingIntent6);
//P4 starts Call4.class
Intent configIntent7 = new Intent(context, Call4.class);
configIntent7.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent7 = PendingIntent.getActivity(context, REQUEST_CODE_SEVEN, configIntent7, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView04, configPendingIntent7);
//P5 starts Call5.class
Intent configIntent8 = new Intent(context, Call5.class);
configIntent8.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent8 = PendingIntent.getActivity(context, REQUEST_CODE_EIGHT, configIntent8, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView05, configPendingIntent8);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId });
}
}
else
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
//Log.e("Error", "msg = null");
}
}
super.onReceive(context, intent);
}
}
}
我还有一个EditPreferences.java、GlobalVars.java 和其他一些现在毫无意义的课程。类的名称不言自明。
另一件事。我也有Widgetmain.java:
public class WidgetMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetmain2);
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain2);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
编辑:这个怎么样:
当我在同事的 ZTE Blade 上安装此应用程序时,小部件上的文本视图未加载适当的文本,仅加载了 strings.xml 中确定的文本。
当我重新安装应用程序(不卸载)时,文本视图已加载并且一切正常。 这个问题在我的 HTC Desire HD 上没有出现。
文本视图像这样(部分代码)加载到上述 UpdateService.java 中:
RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);
updateViews.setTextViewText(R.id.widget_textview, name);
ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
即使“名称”是静态的(例如 String name="Something"),第一次安装时仍不会加载该文本视图。
【问题讨论】:
-
我在这个问题上开了一个赏金,因为我在几周内没有取得任何进展。
-
为什么你从来没有在你的更新函数中调用 super.onUpdate()?
-
我称之为,我只是不知何故从代码中错过了它。我尝试将它作为 onUpdate 函数的第一行和最后一行: super.onUpdate(context, appWidgetManager, appWidgetIds);
-
好的,抱歉。我更仔细地阅读了您的解决方案。我不明白的是你所说的“更新服务”循环。您的服务到底在做什么?看,我的小部件中不需要服务。我是否应该设置一个计时器来定期检查小部件是否需要刷新?
标签: android widget sharedpreferences