【发布时间】:2014-04-21 13:31:53
【问题描述】:
应用程序的快速说明。这是一个简单的小部件,其中 4 个值通过 SharedPreferences 保存。唯一的活动视图是设置首选项的地方。完成后,将保存变量,并更新小部件。小部件每 90,000 毫秒自行更新一次。这一切都很好。我遇到的问题是方向改变时。发生这种情况时,小部件将被销毁,然后重新制作。我得到的是原始 XML,其中可变文本字段设置为默认值。在下一次手动或自动更新之前一直保持这种状态。
编辑: 这个问题现在有了很大的改变。我得到的两个回复指出了一些错误的地方。阅读所有 cmets 以查看所有内容。在最后一次尝试之后,即尝试在清单中添加标签后,我得到了更糟糕的结果。该程序根本无法运行。
我将使用提供该想法的同一答案中所说的最后一句话。我的 onUpdate() 一定有问题。这是整个类的代码:
public class WatchWidget extends AppWidgetProvider
{
public void onCreate(Context context, Bundle savedInstanceState)
{
RemoteViews remoteViews;
remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
remoteViews.setTextViewText( R.id.widget_elapsedtime, "Time");
}
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews remoteViews;
ComponentName watchWidget;
DateFormat format = SimpleDateFormat.getTimeInstance( SimpleDateFormat.MEDIUM, Locale.getDefault() );
remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
watchWidget = new ComponentName( context, WatchWidget.class );
//remoteViews.setTextViewText( R.id.widget_textview, "" + format.format( new Date()));
//widget_date
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(c.getTime());
//remoteViews.setTextViewText( R.id.widget_date, formattedDate);
SharedPreferences prefs = context.getSharedPreferences("qsPrefs", context.MODE_PRIVATE);
String sdayTextEdit = prefs.getString("dayTextEdit", "23");
//String sdayTextEdit ="23";
String smonthTextEdit = prefs.getString("monthTextEdit", "3");
String syearTextEdit = prefs.getString("yearTextEdit", "2014");
String scostTextEdit = prefs.getString("costTextEdit", "8.5");
String spacksTextEdit = prefs.getString("packsTextEdit", ".95");
//Starting day
Calendar pastdate = Calendar.getInstance();
//int inputDay = 25;
int inputDay = Integer.valueOf(sdayTextEdit);
int inputMonth = 2; //starts at 0
inputMonth = Integer.valueOf(smonthTextEdit);
int inputMonthAdj = inputMonth-1;
int inputYear = 2014;
inputYear = Integer.valueOf(syearTextEdit);
pastdate.set(Calendar.DAY_OF_MONTH, inputDay);
pastdate.set(Calendar.MONTH, inputMonthAdj);
pastdate.set(Calendar.YEAR, inputYear);
double packsPer = .95;
packsPer = Double.parseDouble(spacksTextEdit.toString());
double perPack = 8.57;
perPack = Double.parseDouble(scostTextEdit.toString());
inputMonthAdj = inputMonth;
remoteViews.setTextViewText( R.id.widget_date, "From: " + inputMonthAdj +"/"+ inputDay +"/"+ inputYear );
SfinalDate = "From: " + inputMonthAdj +"/"+ inputDay +"/"+ inputYear;
Calendar today = Calendar.getInstance();
DecimalFormat dform = new DecimalFormat("0.00");
long dateDiff = today.getTimeInMillis() - pastdate.getTimeInMillis();
long resultDays = dateDiff / (24*60*60*1000);
remoteViews.setTextViewText( R.id.widget_elapsedtime, ""+ resultDays);
double savedmoney = packsPer * perPack * resultDays;
String finalMoney = dform.format(savedmoney);
remoteViews.setTextViewText( R.id.widget_savedmoney, "$"+ finalMoney);
double notSmoked = resultDays * 20 * packsPer;
int finalNotSmoked = (int) Math.round(notSmoked);
remoteViews.setTextViewText( R.id.widget_cigsnot, ""+ finalNotSmoked);
SfinalMoney = "$"+ finalMoney;
SfinalNS = ""+ finalNotSmoked;
SfinalDays = ""+ resultDays;
appWidgetManager.updateAppWidget( watchWidget, remoteViews );
//Show Prefs screen
Intent intent = new Intent(context, preferences.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.LinearLayout01, pendingIntent);
appWidgetManager.updateAppWidget( watchWidget, views );
}
}
已经发布... 可能有一些额外的行试图设置不再相关的东西。但是那里的代码可以工作。 onUpdate() 以自动间隔(90000 毫秒)正常触发。启动以设置首选项的活动也会在关闭时进行调用,这也会很好地更新它。这就是让我相信整个虚空一定没问题的原因。但一定有什么不对劲。因为当屏幕方向改变时,onUpdate() 不会运行。而且,再次,根据第二个答案,无论如何都不应该这样做,因为系统应该自己保留 UI。但事实并非如此。
【问题讨论】:
-
您的小部件提供商的
onUpdate()应该被调用。在onUpdate()中,您应该根据需要设置视图状态。 -
我试过了。但我不知道如何让它着火。也就是说,我尝试将其设置为 super.onUpdate,因此我可以随时调用它。但是我遇到了与所有其他超级玩家描述的相同的错误。此外,似乎没有一个我可以知道的地方会被触发来调用它。
-
你是说改变方向时不会触发
onUpdate()?您可以尝试覆盖onAppWidgetOptionsChanged()并从那里以及从onUpdate()更新小部件状态 -
onUpdate() 不会在方向更改时调用。它仅根据 AppWidgetProvider 中定义的 XML 标签调用。我认为 onCreate() 会触发,但我也没有看到这种情况发生。我没有将 onAppWidgetOptionsChanged() 用于任何事情。我保存的首选项位于自定义活动中。
标签: android android-widget super