3

我为我的应用程序创建了一个小部件。一切正常,小部件中显示的所有数据都正确显示。但是有时,当我运行其他应用程序(例如相机)并开始拍摄视频几秒钟并停止视频并关闭相机时会发生什么,然后突然在我的小部件中显示的所有数据都丢失了,并且在小部件中什么也没有显示. 我不知道在我在 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>

小部件.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));        
        }
    }
}
4

1 回答 1

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));        
    }
}
于 2012-04-27T10:36:56.080 回答