【问题标题】:Intent extras is not updatingIntent extras 未更新
【发布时间】:2014-03-01 00:05:37
【问题描述】:

我正在尝试从浏览器获取字符串。我在 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 "";
}

我第一次尝试分享一切正常

当我再次尝试这样做时,文本没有更新

我认为是因为 onCreate()。所以我将代码移到 onResume() 中。什么都没发生。

根据这个solution我添加了receivedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

没有效果。

我应该怎么做才能额外更新 Intent?

【问题讨论】:

    标签: android android-intent browser


    【解决方案1】:

    在创建 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();
        }
    }
    

    【讨论】:

    • 你让我很开心谢谢!! :)
    • PendingIntent.FLAG_UPDATE_CURRENT
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多