来自widget的getExtras

时间:2013-09-19 10:48:44

标签: android android-widget

我的小部件上有2个按钮 我已经为这两个添加了putExtra个不同的布尔标志 我正在尝试解析主要活动中的getExtra 但是当我点击它们时,它们完全相同(这不是我所期待的)。

看下面的代码,我做错了什么?

mainActivity

的部分代码
 @Override
    protected void onNewIntent(Intent intent) {
        //called when the activity is relaunched by a new intent
        setIntent(intent);
        super.onNewIntent(intent);
    }



@Override
public void onResume() {

    super.onResume();
    Intent testI = getIntent();
    Bundle test = testI.getExtras();
    if (test != null) {
        boolean select = test.getBoolean("selectcontact",false);
        if (select == true) {
            pickContact();
        }
    }
}

来自AppWidgetProvideronUpdate函数)

的部分代码
        Intent mainIntent = new Intent(ctxt, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("selectcontact", true);
        mainIntent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(ctxt, 0, mainIntent, 0);
        widget.setOnClickPendingIntent(R.id.buttonAddwidget, pendingIntent);

        Intent mainIntentE = new Intent(ctxt, MainActivity.class);
        PendingIntent pendingIntentE = PendingIntent.getActivity(ctxt, 0, mainIntentE, 0);
        widget.setOnClickPendingIntent(R.id.buttonEditwidget, pendingIntentE);

2 个答案:

答案 0 :(得分:0)

// replace this code
@Override
public void onResume() {

    super.onResume();
    Intent testI = getIntent();
    Bundle test = testI.getExtras();
    if (test.getBoolean("selectcontact",false)) {
            pickContact();
    }
}

答案 1 :(得分:0)

对于2个意图,您使用相同的ID。 尝试使用不同的:

PendingIntent pendingIntent  = PendingIntent.getActivity(ctxt, 0, mainIntent, 0);
PendingIntent pendingIntentE = PendingIntent.getActivity(ctxt, 1, mainIntentE, 0);