アプリのウィジェットを作成しました。すべて正常に動作し、ウィジェットに表示されるすべてのデータが正しく表示されます。しかし、カメラなどの他のアプリケーションを実行して数秒間ビデオの撮影を開始し、ビデオを停止してカメラを閉じると、ウィジェットに表示されているすべてのデータが突然失われ、ウィジェットに何も表示されない場合があります。 。Android 2.3.3でアプリを実行する前に問題が発生したかどうかはわかりませんが、スマートフォンをAndroid 4.0に更新すると、この問題が発生します。
失われたデータは、ウィジェットが私が設定した時間間隔に従って更新されると再び表示されます。だから私の質問は、なぜこのデータ損失が発生するのかということです。これは私のコードでしなければならないことです。これを解決するのを手伝ってください。
私が使用したコード
Widget_app_provider.xmlファイル
<?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ファイル
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/appwidget_bg_clickable" >
<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>
</RelativeLayout>
Widget.javaファイル
public class Widget extends AppWidgetProvider implements Utilities
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
// Read data from register before updating the widget
CycleManager.getSingletonObject().readHistoryDateFromFile(context);
CycleManager.getSingletonObject().ComputeAverages();
// To prevent any ANR timeouts, we perform the update in a service
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service
{
@Override
public void onStart(Intent intent, int startId)
{
// Build the widget update for today
RemoteViews updateViews = getValueFromCycleManager(this);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
@Override
public IBinder onBind(Intent arg0){
// TODO Auto-generated method stub
return null;
}
public RemoteViews getValueFromCycleManager(Context context)
{
RemoteViews remoteViews = null;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Date currentDate = calInstance.getTime();
// set No of days remaining for next cycle start to the widgets
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));
}
}
}