简体   繁体   English

Intent.putExtra() 不更新其值

[英]Intent.putExtra() dont update its value

I have 3 buttons in my home screen widget.我的主屏幕小部件中有 3 个按钮。 After click i want to launch my activity with some parameter (it depends which button was clicked).单击后,我想使用某些参数启动我的活动(这取决于单击了哪个按钮)。 The problem is that value is always 0 (it should be 0, 1 or 2, depends on which button was clicked).问题是值始终为 0(它应该是 0、1 或 2,取决于单击的按钮)。 My code looks like this:我的代码如下所示:

for (int appWidgetId : appWidgetIds) {
   RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_app_widget); 

   for(int z=0;z<widget_buttons.length;z++) {
      int id = Integer.valueOf(widget_buttons[z]);
      if(z<accounts_array.length) {
          String[] data_array = accounts_array[z].split("\\,");

          Intent intent = new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          Log.e(APP_TAG, "## ACCOUNT_ID: "+ data_array[0]);
          intent.putExtra("accountID", data_array[0]);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          views.setOnClickPendingIntent(id, pendingIntent);
          views.setTextViewText(id, data_array[1]);
       } else {
          views.setViewVisibility(id,View.INVISIBLE); //hide button
       }
    }

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

First loop is for update every widget, second is for registering click event on 3 buttons.第一个循环用于更新每个小部件,第二个循环用于在 3 个按钮上注册点击事件。

This is how I retrive data in MainActivity.java这就是我在 MainActivity.java 中检索数据的方式

@Override
public void onCreate(Bundle savedInstanceState)
{
    // Log.e(APP_TAG, getExtra("accountID"));

    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();

    String accountParam = "";
    if (extras != null) {
        Log.e(APP_TAG, "AccountID in MainActivity: "+extras.getInt("accountID"));
        accountParam = "?accountID="+extras.getInt("accountID"); //<---
    }

    if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
        moveTaskToBack(true);
    }

    // Set by <content src="index.html" /> in config.xml
    loadUrl("file:///android_asset/www/index.html"+accountParam);
}

Can you please tell why every button after click gives 0 from extras.getInt()?你能告诉为什么点击后的每个按钮都从 extras.getInt() 给出 0 吗?

You pass in the account id as a String (because data is of type String[] ) but in the Activity you try to retrieve it from the extras Bundle with getInt() .您将帐户 ID 作为String传递(因为数据String[]类型),但在Activity您尝试使用getInt()从 extras Bundle检索它。

I set up a small sample app and found the following lines in Logcat:我设置了一个小示例应用程序,并在 Logcat 中找到了以下几行:

01-21 18:24:02.525 4081-4081/com.example.todolist W/Bundle: Key accountID expected Integer but value was a java.lang.String. 01-21 18:24:02.525 4081-4081/com.example.todolist W/Bundle:Key accountID 应为整数,但值为 java.lang.String。 The default value 0 was returned.返回了默认值 0。

01-21 18:24:02.525 4081-4081/com.example.todolist W/Bundle: Attempt to cast generated internal exception: 01-21 18:24:02.525 4081-4081/com.example.todolist W/Bundle:尝试投射生成的内部异常:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at android.os.BaseBundle.getInt(BaseBundle.java:801) at android.os.BaseBundle.getInt(BaseBundle.java:783) at com.example.todolist.OtherActivity.onCreate(OtherActivity.java:24) at android.app.Activity.performCreate(Activity.java:6237) java.lang.ClassCastException: java.lang.String 不能被转换为 java.lang.Integer at android.os.BaseBundle.getInt(BaseBundle.java:801) at android.os.BaseBundle.getInt(BaseBundle.java:783)在 com.example.todolist.OtherActivity.onCreate(OtherActivity.java:24) 在 android.app.Activity.performCreate(Activity.java:6237)

So in cases like this, the app won't crash but you will also not be able to retrieve the correct value.因此,在这种情况下,应用程序不会崩溃,但您也将无法检索正确的值。

In order to fix your code, you should use getString("accountID") .为了修复您的代码,您应该使用getString("accountID")

Another thing: your PendingIntent s all have the same request code (0).另一件事:您的PendingIntent都具有相同的请求代码 (0)。 Because of this, they will be considered as "the same PendingIntent".因此,它们将被视为“相同的 PendingIntent”。 You have to use different request codes or all three Button s will trigger the same behavior.您必须使用不同的请求代码,否则所有三个Button将触发相同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM