2017-01-07 171 views
0

我在我的android應用程序中創建了一個小部件。我連接到數據庫並獲得一些列值。我希望這個小部件每10秒更新一次,並從我的數據庫中獲取下一個值。 我想用cursor.movetonext()來做,但我不確定如何在這裏使用它。如何在android中使用cursor.movetonext()更新主屏幕小部件?

這是我的小部件的java類:

public class DataAppWidget extends AppWidgetProvider { 

    private String dataP; 
    private String dataE; 
    private dataRepo dataRepo; 
    private Cursor cursor; 


    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     for (int appWidgetId : appWidgetIds) { 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.data_app_widget); 
      dataRepo = new dataRepo(context.getApplicationContext()); 

       cursor = dataRepo.getallData(); 
       dataP = cursor.getString(cursor.getColumnIndex(data_P.KEY_P_NAME)); 
       dataE = cursor.getString(cursor.getColumnIndex(data_E.KEY_E_NAME)); 
       views.setTextViewText(R.id.appwidget_p, dataP); 
       views.setTextViewText(R.id.appwidget_e, dataE); 
       appWidgetManager.updateAppWidget(appWidgetId, views); 
     } 
    } 

    @Override 
    public void onEnabled(Context context) { 
     // Enter relevant functionality for when the first widget is created 
    } 

    @Override 
    public void onDisabled(Context context) { 
     // Enter relevant functionality for when the last widget is disabled 
    } 

} 

這是我的小部件信息的xml:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialKeyguardLayout="@layout/data_app_widget" 
    android:initialLayout="@layout/data_app_widget" 
    android:minHeight="40dp" 
    android:minWidth="180dp" 
    android:previewImage="@drawable/data_appwidget_preview" 
    android:resizeMode="horizontal|vertical" 
    android:updatePeriodMillis="10000" 
    android:widgetCategory="home_screen"></appwidget-provider> 

回答

0

好吧,我解決我的問題。

我創建了新的首選項,然後使用SharedPreference我存儲了計數編號。

然後,每次小部件更新我從SharedPreference獲得我的號碼,並使用cursor.move(mynumber),之後我將mynu​​mber值設置爲+1並再次存儲它。