带有3个按钮的Android小部件。和不同的意图

时间:2012-05-08 10:31:45

标签: android android-intent android-widget

我正在尝试创建一个Widget的奇怪问题。 小部件是一个随机问题,有3个答案选项(按钮)。 我从互联网上加载了这个问题并且有效。

我混淆了答案,所以答案并不总是在同一个地方。 此外,可以有多个正确的答案。

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;
}

问题是,当我从新的Activity中执行GetExtra时,它将始终是在wrongIntentPending中设置的额外内容。这里出现问题。但我确实看到“if(correctA == 1){} else {}”的工作正确。 即使我将其设置为“if(correctA == 1){} else {}”,然后我从wrongIntentPending中获取Extra。

出了什么问题?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:3)

我在阅读此页面后发现了这个问题:http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269

工作代码:

// 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, 1, wrongIntent, PendingIntent.FLAG_UPDATE_CURRENT);

在“PendingIntent getActivity(Context context,int requestCode,Intent intent,int flags)”中,我需要设置2个不同的requestCodes。即使谷歌开发者网页上说它目前还没有使用过。

  • context:PendingIntent应该启动的Context 活性。
  • requestCode:发件人的私人请求代码(目前 没用过。)
  • intent:要启动的活动的意图。
  • flags:可以是FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,或者支持的任何标志 Intent.fillIn()控制意图的未指定部分 可以在实际发送时提供。

所以我改变了这一行:

PendingIntent wrongIntentPending = PendingIntent.getActivity(maincontext, 1 ,wrongIntent,PendingIntent.FLAG_UPDATE_CURRENT);

现在它有效。