清算意向 [英] Clearing intent
问题描述
我的 Android 应用正在被传递信息的意图调用(状态栏中的待处理意图).
My Android app is getting called by an intent that is passing information (pendingintent in statusbar).
当我点击主页按钮并通过按住主页按钮重新打开我的应用程序时,它会再次调用意图并且相同的附加功能仍然存在.
When I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
这是不按预期运行的代码
this is the code that doesn't run like its supposed to
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
}
}
还有我的意图:
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或者检查之前是否使用过?
Is there any way to clear the intent or check whether it has been used before?
推荐答案
更新:
当我 5 年前第一次写这个答案时,我没有意识到会被这么多人提及!
UPDATE:
I didn't realise this answer would be referred to so much when I first wrote it more then 5 years ago!
我会澄清指出,根据@tato-rodrigo 的回答,在某些情况下,这不会帮助您检测已处理的意图.
I'll clarify to point out that as per @tato-rodrigo answer this won't help you detect an already handled intent in some situations.
另外我应该指出,我将清除"放在引号中是有原因的 - 你没有通过这样做真正清除意图,你只是将额外的删除用作标志活动已经看到了这个意图.
Also I should point out I put "clear" in quotes for a reason - you are not really clearing the intent by doing this, you're just using the removal of the extra as a flag that this intent has been seen by the activity already.
我遇到了完全相同的问题.
I had exactly the same issue.
以上答案让我走上正轨,我找到了更简单的解决方案,使用:
Above answer put me on the right track and I found even simpler solution, use the:
getIntent().removeExtra("key");
清除"意图的方法调用.
method call to "clear" the Intent.
自从一年前提出这个问题以来,回答有点晚了,但希望这能在未来帮助其他人.
Its a bit late answering since this was asked a year ago, but hopefully this helps others in the future.
这篇关于清算意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!