小部件数据在繁重的应用程序运行时丢失

时间:2012-04-27 08:24:16

标签: android android-widget

我为我的应用创建了一个小部件。一切正常,小部件中显示的所有数据都正确显示。但有时每当我运行其他应用程序(如摄像头)并开始拍摄视频几秒钟并停止视频并关闭相机时,会突然发生在我的窗口小部件中显示的所有数据都丢失,并且在窗口小部件中没有显示任何内容。在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文件

<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文件

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));        
    }
}
}

1 个答案:

答案 0 :(得分:0)

尝试实施IntentService,以便更好地处理被杀事件。您需要使用onHandleIntent中指定的意图调用context.startService(new Intent(context, UpdateService.class));中的工作。

public static class UpdateService extends IntentService 
{

    public UpdateService() 
    {
        super("UpdateService");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        // 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);
    }

    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));        
    }
}