2017-08-02 4 views
-1

J'ai fait un widget qui est censé afficher un proverbe au hasard chaque jour jusqu'à présent, tout fonctionne bien, sauf que le proverbe ne change pas, for testing purposes I set the timer for 3 secondesproverbes Afficher tous les jours dans un widget - Android

Voici mon code:

JAVA

public class ProverbsWidget extends AppWidgetProvider { 

public static int TIME_OUT = 3000; //1000 = 1 sec 
public static String fnl_hv = RandomizedProverb(); //this is the method that generates the randem proverb and it's working fine 

static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, 
          final int appWidgetId) { 

     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       // This method will be executed once the timer is over 
       String widgetText = fnl_hv; 

       // Construct the RemoteViews object 
       RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.proverbs_widget); 
       views.setTextViewText(R.id.sayings, widgetText); 

       // Instruct the widget manager to update the widget 
       appWidgetManager.updateAppWidget(appWidgetId, views); 
      } 
     },TIME_OUT); 
} 

Le RandomizeProverb():

public static String RandomizedProverb(){ 
    Random rand = new Random(); 
    String [] Quotes = new String []{ 
      "test 1", 
      "test 2", 
      "test 3", 
      "test 4", 
      "test 5", 
      "test 6", 
      "test 7", 
      "test 8", 
      "test 9", 
      "test 10", 
      "test 11", 
      "test 12", 
      "test 13", 
      "test 14", 
      "test 15", 
      "test 16", 
      "test 17", 
      "test 18", 
      "test 19", 
      "test 20", 
    }; 

    String havm = Quotes[rand.nextInt(20)]; 
    String hav = havm; 
    return hav; 
} 

Il est clair que le problème est dans mon horloge, mais je ne peux pas sembler le faire fonctionner

+0

C'est parce que vous obtenez seulement la chaîne randomisée sur la création de classe. Et ne jamais le changer dans votre Runnable. Donc, il reste tel qu'il était à l'origine, pour toujours. –

+0

@ModularSynth pouvez-vous proposer un correctif pour cela? –

+0

De toute évidence, copiez ce 'fnl_hv = RandomizedProverb();' dans la méthode 'run()'. –

Répondre

1

Ceci est résolu en accédant au fichier XML et définir la valeur de updatePeriodMillis-86400000 comme ceci:

android:updatePeriodMillis="86400000" 

Fondamentalement, cela indique le widget quand se mettre à jour et quand le temps est la méthode updateAppWidget() sera exécuté et puisque 86400000 est le nombre de millisecondes qui existent dans 24 heures, le widget sera mis à jour quotidiennement

0

le faire comme ceci:

Handler handler = new Handler(); 
Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       // This method will be executed once the timer is over 
       String widgetText = fnl_hv; 

       // Construct the RemoteViews object 
       RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.proverbs_widget); 
       views.setTextViewText(R.id.sayings, widgetText); 

       // Instruct the widget manager to update the widget 
       appWidgetManager.updateAppWidget(appWidgetId, views); 
       handler.postDelayed(runnable, timetoswitch); 
      } 
     },InitialDelayTime; 

Hope it helps !!!

+0

Non, cela n'a pas fonctionné –