gpt4 book ai didi

android - 清算 Intent

转载 作者:IT老高 更新时间:2023-10-28 13:05:50 31 4
gpt4 key购买 nike

我的 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 的其他人有所帮助。

关于android - 清算 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4116110/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com