Intent extras没有更新

Joe*_*mov 4 browser android android-intent

我试图从浏览器中获取字符串.我在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>
Run Code Online (Sandbox Code Playgroud)

在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 "";
}
Run Code Online (Sandbox Code Playgroud)

我第一次尝试分享一切正常 在此输入图像描述

当我试图再次这样做时,文本不会更新 在此输入图像描述

我认为这是因为onCreate().所以我将代码移到onResume()中.没啥事儿.

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

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    this.setIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)

没有效果.

我该怎么办才能更新意图?

ste*_*fan 5

在创建PendingIntent时使用FLAG_UPDATE_CURRENT来更新新Intent的额外内容.

没有删除/替换意图的额外内容

要么

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();
    }
}
Run Code Online (Sandbox Code Playgroud)