I created a widget for my application. Everything works fine, and all the data displayed in the widget is displayed correctly. But sometimes what happens when I launch some other application, such as a camera, and start shooting a video for several seconds, stopping the video and closing the camera, suddenly all the data displayed in my widget is lost and does not show anything in widgets. I do not know if a problem occurs before I launch the application in Android 2.3.3, but as I upgrade my phone to Android 4.0, this problem shows.
Lost data appears again when the widget is updated in accordance with the time interval I set. So my question is why this data loss occurs, this is what I should do in my code. Please help me solve this problem.
The code I used
Widget_app_provider.xml File
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="56dp"
android:minWidth="56dp"
android:updatePeriodMillis="2100000" />
WidgetLayout.xml File
<ImageView
android:id="@+id/backgroundImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/tracking_status_faq_imageview_text"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal|center_vertical" >
<TextView
android:id="@+id/txt_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5f5f5f"
android:textSize="14dp" >
</TextView>
</LinearLayout>
Widget.java File
public class Widget extends AppWidgetProvider implements Utilities
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
CycleManager.getSingletonObject().readHistoryDateFromFile(context);
CycleManager.getSingletonObject().ComputeAverages();
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service
{
@Override
public void onStart(Intent intent, int startId)
{
RemoteViews updateViews = getValueFromCycleManager(this);
ComponentName thisWidget = new ComponentName(this, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
@Override
public IBinder onBind(Intent arg0){
return null;
}
public RemoteViews getValueFromCycleManager(Context context)
{
RemoteViews remoteViews = null;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Date currentDate = calInstance.getTime();
Date dtStart = CycleManager.getSingletonObject().getStartDate();
int iAvgCycleLength = CycleManager.getSingletonObject().getAvgCycleTime();
calInstance.setTime(dtStart);
calInstance.add(Calendar.DATE, iAvgCycleLength);
Date dtNextCycleDate = calInstance.getTime();
Long noOfDaysLeft = dtNextCycleDate.getTime()- currentDate.getTime();
noOfDaysLeft = ((noOfDaysLeft / (1000 * 60 * 60)) + 1) / 24;
remoteViews.setTextViewText(R.id.txt_date, Long.toString(noOfDaysLeft));
}
}
}
source
share