I have a strange issue with a Widget that I am trying to create.
The widget is a random question with 3 answer options (buttons).
I load the question from the internet and that works.
I mix up the answers, so the answer is not always in the same location.
Also, it is possible to have multiple correct answers.
public RemoteViews setAnswers(JSONObject questionObj, RemoteViews views) throws JSONException {
// Get the Answers
String answerA = questionObj.getString("Ans1");
String answerB = questionObj.getString("Ans2");
String answerC = questionObj.getString("Ans3");
// Get the Answers that is correct (1 = Correct, 0 = Wrong)
int correctA = Integer.parseInt(questionObj.getString("correct1"));
int correctB = Integer.parseInt(questionObj.getString("correct2"));
int correctC = Integer.parseInt(questionObj.getString("correct3"));
// Get help text for correct or wrong answer (HTML)
String HTTPcor = questionObj.getString("HTTPcor")+" - Correct";
String HTTPwro = questionObj.getString("HTTPwro")+" - Wrong";
// Create the Intent for the answers that is correct
Intent correctIntent = new Intent(maincontext, AdMain.class);
correctIntent.putExtra("appWidID", appWidId);
correctIntent.putExtra("correct", 1); // Answer is correct.
correctIntent.putExtra("HTTP", HTTPcor); // Help text.
PendingIntent correctIntentPending = PendingIntent.getActivity(maincontext, 0, correctIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create the Intent for the answers that is wrong
Intent wrongIntent = new Intent(maincontext, AdMain.class);
wrongIntent.putExtra("appWidID", appWidId);
wrongIntent.putExtra("correct", 0); // Answer is wrong.
wrongIntent.putExtra("HTTP", HTTPwro); // Help text.
PendingIntent wrongIntentPending = PendingIntent.getActivity(maincontext, 0, wrongIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create a random number between 1 and 6 (6 different ways to mix up 3 buttons)
Random generator = new Random();
int mix = generator.nextInt(6) + 1;
Log.v("JapanWidget", "AnswerMix - "+mix+" - appWidId = "+appWidId);
// DEBUG Set mix to 1 Until i get the Intent fixed.
mix = 1;
// Mix the buttons.
switch(mix) {
case 1:
views.setTextViewText(R.id.Answer1, answerA); // Button 1 answer A
// If correctA = 1 THEN set correctIntentPending ELSE set wrongIntentPending
if (correctA == 1) { views.setOnClickPendingIntent(R.id.Answer1, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer1, wrongIntentPending); }
views.setTextViewText(R.id.Answer2, answerB); // Button 2 answer B
// If correctB = 1 THEN set correctIntentPending ELSE set wrongIntentPending
if (correctB == 1) { views.setOnClickPendingIntent(R.id.Answer2, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer2, wrongIntentPending); }
views.setTextViewText(R.id.Answer3, answerC); // Button 3 answer C
// If correctB = 1 THEN set correctIntentPending ELSE set wrongIntentPending
if (correctC == 1) { views.setOnClickPendingIntent(R.id.Answer3, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer3, wrongIntentPending); }
break;
default:
views.setTextViewText(R.id.Answer1, answerA);
if (correctA == 1) { views.setOnClickPendingIntent(R.id.Answer1, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer1, wrongIntentPending); }
views.setTextViewText(R.id.Answer2, answerB);
if (correctB == 1) { views.setOnClickPendingIntent(R.id.Answer2, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer2, wrongIntentPending); }
views.setTextViewText(R.id.Answer3, answerC);
if (correctC == 1) { views.setOnClickPendingIntent(R.id.Answer3, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer3, wrongIntentPending); }
break;
}
return views;
}
The problem is, that when i do GetExtra from the new Activity, then it will always be the the extra that is set in the wrongIntentPending. Someting is going wrong here. But I do see that the “if (correctA == 1) { } else { }” works correct.
Even if I set it to “if (correctA == 1) { } else { }”, then I get the Extra from the wrongIntentPending.
What is going wrong? Or is there a better way of doing this?
I found the issue, after reading this page: http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269
Working code:
In the “PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)” i needed to set 2 different requestCodes. Even is the google developer pages say that it is not currently used.
activity.
not used).
FLAG_UPDATE_CURRENT, or any of the flags as supported by
Intent.fillIn() to control which unspecified parts of the intent that
can be supplied when the actual send happens.
So I changed this line:
PendingIntent wrongIntentPending = PendingIntent.getActivity(maincontext, 1, wrongIntent, PendingIntent.FLAG_UPDATE_CURRENT);
And now it works.