清除意图意图

2023-09-12 08:41:14 作者:闭眼看生活

我的Andr​​oid应用程序获取调用由正在传递信息(pendingintent在状态栏)的意图。

My Android app is getting called by an intent that is passing information (pendingintent in statusbar).

当我打的回家按钮,按住home键再次调用的意图和相同的演员仍然存在重新打开我的应用程序。

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

这是在code不运行像它应该

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

有没有什么办法来清除意图或检查它是否已被使用过?

Is there any way to clear the intent or check whether it has been used before?

推荐答案

我有完全一样的问题。

上面的回答让我在​​正确的轨道上,我发现了更简单的解决方案,使用:

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.

 
精彩推荐
图片推荐