意图附加项未更新

4

我正在尝试从浏览器获取字符串。我在AndroidManifest.xml中使用了SEND操作。

<intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
</intent-filter>

在MainActivity.java中,我有以下代码
@Override
public void onResume() {
    super.onResume();
    Intent receivedIntent=getIntent();
    etSearch.setText(getStringFromIntent(receivedIntent));
}

public String getStringFromIntent(Intent receivedIntent) {
    // get the action
    String receivedAction = receivedIntent.getAction();
    // find out what we are dealing with
    String receivedType = receivedIntent.getType();
    if (receivedAction.equals(Intent.ACTION_SEND)) {
        if (receivedType.startsWith("text/")) {
            // get the received text
            String receivedText = receivedIntent
                    .getStringExtra(Intent.EXTRA_TEXT);
            // check we have a string
            if (receivedText != null) {
                // set the text
                return receivedText.trim();
            }
        }
    }
    return "";
}

我第一次尝试分享时,一切正常 enter image description here

当我再次尝试时,文本没有更新 enter image description here

我认为这是由于onCreate()引起的。所以我将代码移动到了onResume()中。但是仍然没有任何变化。

根据这个解决方案,我添加了receivedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    this.setIntent(intent);
}

没有效果。

为了更新意图附加信息,我应该怎么做?

1个回答

7

在创建PendingIntent时,使用FLAG_UPDATE_CURRENT以便从新的Intent中更新附加信息。

Intent extras not removed/replaced

或者

PendingIntent.FLAG_ONE_SHOT

private void send(Intent intentShowApp) {
    PendingIntent showAppPendingIntent = PendingIntent.getActivity(context, 0, intentShowApp, PendingIntent.FLAG_ONE_SHOT);
    try {
        showAppPendingIntent.send();
    } catch (CanceledException e) {
        e.printStackTrace();
    }
}

你让我开心了!谢谢 :) - Rida Shamasneh
PendingIntent.FLAG_UPDATE_CURRENT - behelit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接