android – Intent extras没有更新

android – Intent extras没有更新,第1张

概述我试图从浏览器中获取字符串.我在 AndroidManifest.xml中使用了SEND *** 作 <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> 我试图从浏览器中获取字符串.我在 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中,我有以下代码

@OverrIDepublic 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);}

没有效果.

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

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

Intent extras not removed/replaced

要么

PendingIntent.FLAG_ONE_SHOT

private voID send(Intent intentShowApp) {    PendingIntent showAppPendingIntent = PendingIntent.getActivity(context,intentShowApp,PendingIntent.FLAG_ONE_SHOT);    try {        showAppPendingIntent.send();    } catch (CanceledException e) {        e.printstacktrace();    }}
总结

以上是内存溢出为你收集整理的android – Intent extras没有更新全部内容,希望文章能够帮你解决android – Intent extras没有更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129389.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存