android - 清算 Intent

我的 Android 应用被传递信息的 Intent 调用(状态栏中的待处理 Intent )。

当我点击主页按钮并按住主页按钮重新打开我的应用程序时,它会再次调用 Intent ,并​​且仍然存在相同的附加功能。

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
    }

这是不按预期运行的代码

    String imgUrl;
    Bundle extras = this.getIntent().getExtras();


    if(extras != null){
        imgUrl = extras.getString("imgUrl");
        if( !imgUrl.equals(textView01.getText().toString()) ){

            imageView.setImageDrawable( getImageFromUrl( imgUrl ) );
            layout1.setVisibility(0);
            textView01.setText(imgUrl);//textview to hold the url

        }

    }

我的 Intent :

public void showNotification(String ticker, String title, String message, 
    String imgUrl){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = 
        (NotificationManager) getSystemService(ns);
    int icon = R.drawable.icon;        // icon from resources
    long when = System.currentTimeMillis();         // notification time
    CharSequence tickerText = ticker;              // ticker-text

    //make intent
    Intent notificationIntent = new Intent(this, activity.class);
    notificationIntent.putExtra("imgUrl", imgUrl);
    notificationIntent.setFlags(
        PendingIntent.FLAG_UPDATE_CURRENT | 
        PendingIntent.FLAG_ONE_SHOT);
    PendingIntent contentIntent = 
        PendingIntent.getActivity(this, 0, 
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | 
        PendingIntent.FLAG_ONE_SHOT);

    //make notification
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(this, title, message, contentIntent);
    //flags
    notification.flags = Notification.FLAG_SHOW_LIGHTS | 
        Notification.FLAG_ONGOING_EVENT | 
        Notification.FLAG_ONLY_ALERT_ONCE | 
        Notification.FLAG_AUTO_CANCEL;
    //sounds
    notification.defaults |= Notification.DEFAULT_SOUND;
    //notify
    mNotificationManager.notify(1, notification);
}

有什么办法可以清除intent或者检查之前是否使用过?

最佳答案

更新:

当我 5 年前第一次写这个答案时,我没有意识到这个答案会被引用这么多!

我会澄清指出,根据@tato-rodrigo 的回答,这在某些情况下不会帮助您检测已经处理的 Intent 。

另外我应该指出,我将“清除”放在引号中是有原因的 - 你不是这样做真的清除了 Intent ,你只是使用删除额外的作为标志Activity 已经看到了这个 Intent 。


我遇到了完全相同的问题。

以上答案让我走上了正确的道路,我找到了更简单的解决方案,请使用:

getIntent().removeExtra("key"); 

方法调用以“清除” Intent。

自从一年前提出这个问题以来,回答有点晚了,但希望这对 future 的其他人有所帮助。

https://stackoverflow.com/questions/4116110/

相关文章:

android - post方法到底是做什么的?

android - 将 largeHeap 设置为 true 有什么好处?

android - 如何调试签署发布的apk?

android - ListView addHeaderView 导致位置加一?

android - 是否可以更改android单选按钮组中的单选按钮图标

android - 从 android 图库中选择多张图片

android - 在 Android 中通过 Canvas 创建一个空位图并进行绘图

android - 如何在 Android 中更改代理设置(尤其是在 Chrome 中)

android - ViewPager.setOffscreenPageLimit(0) 无法按预期

android - 如何查看 Google Play 服务版本?