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

标签 android android-intent android-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 中设置的 extra。这里出了点问题。但我确实看到“if (correctA == 1) { } else { }”工作正常。 即使我将它设置为“if (correctA == 1) { } else { }”,我也会从 wrongIntentPending 中获得 Extra。

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

最佳答案

我在阅读此页面后发现了问题: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 个不同的请求代码。即使是谷歌开发者页面也说它目前没有被使用。

  • context:这个 PendingIntent 应该启动的上下文 Activity 。
  • requestCode:发件人的私有(private)请求代码(目前 未使用)。
  • intent:要启动的 Activity 的 Intent 。
  • 标志:可能是 FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT、 FLAG_UPDATE_CURRENT,或任何支持的标志 Intent.fillIn() 控制 Intent 的哪些未指定部分 可以在实际发送发生时提供。

所以我改变了这一行:

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

现在可以了。

关于带有 3 个按钮的 Android 小部件。和不同的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10496892/

相关文章:

android - 图片未覆盖完整图片 View

android - Android 的正向地理编码示例代码?

android - 如何让我的 Android 应用程序在后台保持 Activity 状态以进行 24/7 连续传感器数据收集

android - 从小部件启动 Activity

android - 尝试动态插入相对布局时获取 ClassCastException?

java - LibGDX 如何在已有值(value)的基础上增加值(value)?

javascript - Android Webview注入(inject)JS&CSS

java - Android使用 Intent 打开外部目录

delphi - 点击通知后如何返回应用程序?

android - 如何处理 Android 中远程 View 的异常(自定义小部件或自定义通知)?