可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
}
}
And my 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);
}
Is there any way to clear the intent or check whether it has been used before?
回答1:
UPDATE:
I didn't realise this answer would be referred to so much when I first wrote it more then 5 years ago!
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.
回答2:
EDIT: I'm editing to post the complete solution I'm using.
This solution will work if the problem is "Not execute some code when the activity starts from History (Recent Apps)".
First of all, declare a boolean
in your Activity
to indicate if the Intent
was already consumed:
private boolean consumedIntent;
Then, safely store and restore this value using the onSaveInstanceState
and onCreate
methods to handle configuration changes and cases that the system may kill your Activity
when it goes to background.
private final String SAVED_INSTANCE_STATE_CONSUMED_INTENT = "SAVED_INSTANCE_STATE_CONSUMED_INTENT";
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_INSTANCE_STATE_CONSUMED_INTENT, consumedIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set content view ...
if( savedInstanceState != null ) {
consumedIntent = savedInstanceState.getBoolean(SAVED_INSTANCE_STATE_CONSUMED_INTENT);
}
//other initializations
}
Now, check if you can run your code under onResume
method.
@Override
protected void onResume() {
super.onResume();
//check if this intent should run your code
//for example, check the Intent action
boolean shouldThisIntentTriggerMyCode = [...];
Intent intent = getIntent();
boolean launchedFromHistory = intent != null ? (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0 : false;
if( !launchedFromHistory && shouldThisIntentTriggerMyCode && !consumedIntent ) {
consumedIntent = true;
//execute the code that should be executed if the activity was not launched from history
}
}
Additionally, if your Activity
is configured to singleTop
, you should reset your flag when a new Intent
is delivered.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
consumedIntent = false;
}
回答3:
Maks answer works for clearing an extra:
getIntent().removeExtra("key");
Another useful command is:
getIntent().setAction("");
You could also tag an intent by calling:
getIntent().putExtra("used", true);
and then just check for the value.
回答4:
When we launch Android apps from History (Recent Apps), the app could be launched with primarily three different Intent flags.
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
This is when activity is being launched from history of an app that was minimised (long press home key).
Constant Value: 1048576 (0x00100000)
FLAG_ACTIVITY_NEW_TASK
This is when activity is launched via "clicking application icon" or via "Intent filters". Here the activity will become the start of a new task on this history stack.
Constant Value: 268435456 (0x10000000)
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY | FLAG_ACTIVITY_NEW_TASK
This is when app was quit by pressing back button and then resumed from History (recent apps).
Constant Value: 269484032 (0x10100000)
Constant value can be retrieved via getIntent().getFlags()
In the third case, Android reloads the last Intent values from its memory. So your app's intent (getIntent
) will have values from the last intent that launched the app.
Actually, the app should behave as if it is a fresh launch, with intents values for a fresh launch rather than the intent values of the previous launch. This behaviour can be seen if you launch the app by clicking the app icon, it will never have old intent values. This is because, Android uses the following intent filter for this scenario
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
But in the third case (App that was quit, is launched from History of Recent apps), Android OS uses that last intent that launched the app before it was quit (via pressing back button). So you end up having old intent values and the app flow is not proper.
Removing intent is one way of solving it, but it would not solve the issue completely! As Android OS reloads the Intent from the apps last launch, and not last instance of the launch intent.
A clean way to avoid this from happening, is to handle it via getting the type of Intent to determine the launch type.
So in your LaunchActivity (The one which has the intent filter defined in the manifest), you could use the following code in the onCreate()
, onStart()
, or onResume()
methods.
if(getIntent().getFlags() == (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)) {
//app is launched from recent apps after it was closed
normalLaunch();
} else {
String intentAction = getIntent().getAction();
String scheme = getIntent().getScheme();
//app is launched via other means
// URL intent scheme, Intent action etc
if("https".equalsIgnoreCase(scheme)) {
// URL intent for browser
} else if("com.example.bb".equalsIgnoreCase(intentAction)) {
// App launched via package name
} else {
// App was launched via Click on App Icon, or other means
normalLaunch();
}
}
I presume normalLaunch()
, should not use parameters from the Intent; otherwise you would need to segregate and optimise your default launch method not to use Intent parameters.
回答5:
Clearing an intent object:
intent.replaceExtras(new Bundle());
intent.setAction("");
intent.setData(null);
intent.setFlags(0);
回答6:
Short answer is No way
Long answer. There is no such thing as "one-shot" intent. From experiment it is observed that recent activity history in modern Androids is nothing more than "Intent history". The latest intent passed to activity is simply logged into system and that's the deal. Folks above suggest to use
setAction("")
But it does not work because the intent is already logged till the moment you get it inside onNewIntent() or onStart() method.
I solved the problem by avoiding use of intents. My problem was simmiliar to the one posted by the author. I tried to implement Global Exit from application via control in notification area. It should stop the underlying service and close all activities of the app. You can find same behavior in Waze application.
The algorithm:
- Create PendingIntent for notification control which passes "Exit" action to activity. But to the special activity which is a simple proxy.
- Proxy activity onStart() code parses the intent, checks action and sets the state of some model to "Exited".
- Proxy activity onStart() code clears original intent using setIntent("") and then forwards it to destination "Root" activity by calling startActivity(intent).
- Proxy activity onStart() code invoke finish().
- Inside onStart() and onNewIntent() of destination activity check model state and call finish() if it is "Exited" (and also call stopService() in my case).
I hope this will help to someone because I did not find the answer in the internet.
回答7:
Make sure that you are using PendingIntent.FLAG_UPDATE_CURRENT flag for PendingIntent.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, mPutIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Where mPutIntent
is your Intent
.
Hope this will help you.
回答8:
I've got exactly the same problem. My solution was to add boolean
variable that was set when Intent
was 'used' and if
statement based on this boolean
to check if you should use Intent
or not.
回答9:
When you are done processing the Intent, do this:
setIntent(null);
You won't see that processed Intent again, and you won't be masking the problem by editing the contents of the processed Intent.
回答10:
I recently had this problem and I solved it by adding a timestamp as a extra parameter to the intent:
private void launchActivity(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("KEY_EXTRA_TIMESTAMP", System.currentTimeMillis());
context.startActivity(intent);
}
After that, save the timestamp to the shared preferences:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long time = getIntent().getLongExtra("KEY_EXTRA_TIMESTAMP", -1);
long previousTime = getPreferences(MODE_PRIVATE).getLong("timestamp", -1);
//ignore if the timestamp is the same as the previous one
if (time != previousTime) {
handleIntent(getIntent());
if (time != -1) {
//save the timestamp
getPreferences(MODE_PRIVATE).edit().putLong("timestamp", time).apply();
}
}
}
回答11:
I couldn't find a way to remove Intent Extra. None of the answers about removing extra from intent works if you enable "Do Not Keep Activities" from Developer Options (That way you can destroy Activity and come back to test if Extras are still there).
As a solution to the problem, i stored boolean value in SharedPreferences after Intent Extras are processed. When same Intent is redelivered to the Activity, i check the SharedPreference value and decide to process the Intent Extra. In case you send another new Intent Extra to same Activity, you make SharedPreference value false and Activity will process it. Example:
// Start Activity with Intent Extras
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("someData", "my Data");
// Set data as not processed
context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).edit().putBoolean("myActivityExtraProccessed", false).commit();
context.startActivity(intent);
...
public class MyActivity{
...
public void someMethod(){
boolean isExtrasProcessed = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).getBoolean("myActivityExtraProccessed", false);
if (!isExtrasProcessed) {
// Use Extras
//Set data as processed
context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).edit().putBoolean("myActivityExtraProccessed", true).commit();
}
}
}
回答12:
Even after manually clearing the Intent and Intent extras after they have been parsed, it seems as though Activity.getIntent() will always return the original Intent that started the Activity.
To get around this, I recommend something like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The Intent provided by getIntent() (and its extras) will persist through a restore
// via savedInstance. Because of this, restoring this activity from a
// an instance that was originally started with extras (deep-link or
// pre-defined destination) may cause un-desired behavior
// (ie...infinite loop of sending the user directly to somewhere else because of a
// pre-defined alternate destination in the Intent's extras).
//
// To get around this, if restoring from savedInstanceState, we explicitly
// set a new Intent *** to override the original Intent that started the activity.***
// Note...it is still possible to re-use the original Intent values...simply
// set them in the savedInstanceState Bundle in onSavedInstanceState.
if (savedInstanceState != null) {
// Place savedInstanceState Bundle as the Intent "extras"
setIntent(new Intent().putExtras(savedInstanceState));
}
processIntent(getIntent())
}
private void processIntent(Intent intent) {
if (getIntent().getExtras() == null) {
// Protection condition
return;
}
doSomething(intent.getExtras.getString("SOMETHING_I_REALLY_NEED_TO_PERSIST"));
final String somethingIDontWantToPersist =
intent.getExtras.getString("SOMETHING_I_DONT_WANT_TO_PERSIST");
if(somethingIDontWantToPersist != null) {
doSomething(somethingIDontWantToPersist);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save selective extras from original Intent...
savedInstanceState.putString("SOMETHING_I_REALLY_NEED_TO_PERSIST", "persistedValued");
super.onSaveInstanceState(savedInstanceState);
}
This way, there is a mechanism to dump the original Intent while still retaining the ability to explicitly retain certain parts of the original Intent / Intent extras.
Note that I haven't tested all Activity launch modes.
回答13:
The straight forward way is to avoid calling getIntent() from methods other than onCreate(). But this will cause problem during next launch if the user left our Activity by tapping the Home button. I think this problem don't have a fully functional solution.
回答14:
I confront the same problem and try to use above methods but it doesn't work.
I think it may be cause of acitvity's launch mode which I used singleTop mode.
When I background app and use RamEater to simulate issue that intent always has extra even I set it null or remove key.
The problem is gone by using preference storage on Android to check that had pass through.
回答15:
How about this? Sets newIntent as the intent.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
回答16:
How about when you wish to clear the intent - replace it with an empty one?
eg.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
@Override
public void onResume() {
super.onResume();
Intent theIntent = getIntent();
if ("myaction".equals(theIntent.getAction()) {
handleIntent();
onNewIntent(new Intent()); // <--- "clear" the intent by setting empty one
}
}
回答17:
This will hopefully help everyone.
So first we get the intent
//globally
Intent myIntent;
Place this somewhere onCreate
myIntent = getIntent();
String data = myIntent.getStringExtra("yourdata");
//Your process here
Now lets set this so everytime our app gets destroyed or exited we will remove the data
@Override
protected void onDestroy() {
//TODO: Clear intents
super.onDestroy();
myIntent.removeExtra("data");
}
@Override
protected void onBackPressed() {
//TODO: Clear intents
super.onBackPressed();
myIntent.removeExtra("data");
}
You get the idea, if this is not enough just find more 'on' callbacks
回答18:
While the Intent.removeExtra("key")
will remove one specific key from the extras, there is also the method Intent.replaceExtras(Bundle), which can be used to delete the whole extras from the Intent, if null
is passed as a parameter.
From the docs:
Completely replace the extras in the Intent with the given Bundle of
extras.
Parameters
extras The new set of extras in the Intent, or null to erase all extras.
Because the putXXX() methods initialize the extras with a fresh Bundle if its null, this is no problem.
回答19:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);