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 secondes
proverbes 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
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. –
@ModularSynth pouvez-vous proposer un correctif pour cela? –
De toute évidence, copiez ce 'fnl_hv = RandomizedProverb();' dans la méthode 'run()'. –