2013-04-26 80 views
0

當我的應用程序啓動時,我使用listview和第一項調用活動。我的小部件添加了新的一個項目中的onUpdate()ListView控件:在窗口小部件屏幕添加時不要調用updateAppWidget

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_widget); 
      myString = context.getResources().getStringArray(R.array.quotes); 

      Intent active = new Intent(context, Widget.class); 
      active.setAction(ACTION_WIDGET_RECEIVER); 

      Intent archive = new Intent(context, Widget.class); 
      archive.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      archive.setAction(ACTION_WIDGET_ARCHIVE); 

      String q = myString[rgenerator.nextInt(myString.length)]; 
      active.putExtra("msg", q); 
      if (getQuotes(context).size()>1) 
      remoteViews.setTextViewText(R.id.widget_textview, q); 

      PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
      PendingIntent archivePendingIntent = PendingIntent.getBroadcast(context, 0, archive,PendingIntent.FLAG_UPDATE_CURRENT); 

      remoteViews.setOnClickPendingIntent(R.id.widgetFrameLayout, archivePendingIntent); 
      Time now = new Time(); 
      now.setToNow(); 
      String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 

      // adding new item to the listView 
      saveQuote(context, q, mydate); 
      appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
     } 

,當用戶添加小部件到屏幕上,一個更多的項目增加了自動列出。但是我需要在android:updatePeriodMillis期間過後再添加它。我怎樣才能實現這個?

回答

0

我已經解決了它是這樣的:創建了兩個方法與SharedPreferences,這裏我把的是什麼方法國旗現在執行:

private void setWidgetActive(Context context, boolean active,String field){ 
     Context appContext = context.getApplicationContext(); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); 
     SharedPreferences.Editor edit = prefs.edit(); 
     edit.putBoolean(field, active); 
     edit.commit(); 
    } 
    private boolean getWidgetActive(Context context, String field) 
    { 
     Context appContext = context.getApplicationContext(); 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); 
    return prefs.getBoolean(field, false); 
    } 

然後,我onEnabled方法:

@Override 
    public void onEnabled(Context context) { 
     super.onEnabled(context); 
     setWidgetActive(context,true, WIDGET_ACTIVE); 
    } 

在的onUpdate方法:

@Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     setWidgetActive(context,true, WIDGET_UPDATE); 
     if (getWidgetActive(context, WIDGET_UPDATE) & !getWidgetActive(context, WIDGET_ACTIVE)) 
     { 
      myString = context.getResources().getStringArray(R.array.quotes); 
      q = myString[rgenerator.nextInt(myString.length)]; 
      mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 
      saveQuote(context, q, mydate); 
     } 
     else 
     { 
      ArrayList<Quote> importList = getQuotes(context); 
      q = importList.get(importList.size() - 1).getText(); 
      mydate = importList.get(importList.size() - 1).getDate(); 
      setWidgetActive(context,false, WIDGET_ACTIVE); 

     } 
} 

所以如果剛纔添加窗口小部件,getWidgetActive(背景下,W IDGET_ACTIVE)返回true,我得到最後添加的數據庫字段並將其發送到窗口小部件的TextView。在這種情況下,當定義的時間已過時,我從數組中獲得新的隨機stting並保存在數據庫中,併發送到TextView。

相關問題