Android 在nottification中按下挂起的内容时,如何使用打开的活动

Android 在nottification中按下挂起的内容时,如何使用打开的活动,android,android-service,android-notifications,android-pendingintent,Android,Android Service,Android Notifications,Android Pendingintent,我在打开我的应用程序的左角有通知。它很好用。问题是,每次用户按下Notification按钮时,它都会在内存中再次创建应用程序,但不会关闭旧的应用程序 当然,我可以在第一次触摸后隐藏通知图标,但我不想 有没有办法使用已经打开的同一个应用程序 以下是创建此通知的服务: public class MyShippingService extends Service { NotificationManager nm; String LOG_TAG; int shortDate; @O

我在打开我的应用程序的左角有通知。它很好用。问题是,每次用户按下Notification按钮时,它都会在内存中再次创建应用程序,但不会关闭旧的应用程序

当然,我可以在第一次触摸后隐藏通知图标,但我不想

有没有办法使用已经打开的同一个应用程序

以下是创建此通知的服务:

 public class MyShippingService extends Service {

 NotificationManager nm;
  String LOG_TAG;
  int shortDate;

  @Override
  public void onCreate() {
    super.onCreate();
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  }

  public int onStartCommand(Intent intent, int flags, int startId) {
       shortDate=intent.getIntExtra("shortDate", 1);
       sendNotif();
       return super.onStartCommand(intent, flags, startId);
  }

  void sendNotif() {

    Intent intent1 = new Intent(this, Main.class);
      intent1.setAction("hello");
    intent1.putExtra("notification", true);

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);


    Notification notify = new Notification.Builder(this)
        .setContentIntent(pIntent)
            .setContentText(getString(R.string.nottification))
         .setTicker(getString(R.string.nottification))
         .setSmallIcon(R.mipmap.ic_icon)
         .setWhen(System.currentTimeMillis())
            .build();

    nm.cancel(shortDate);
    nm.notify(shortDate, notify);
  }

  public IBinder onBind(Intent arg0) {
    return null;
  }

}

在清单中将主活动的启动模式设置为
singleTop
。请看下面的图片。当您再次启动活动时,将在
onNewIntent()
方法上收到目的信息

谢谢,它可以工作)单击通知时打开同一活动。但当活动已经打开时,不是从nottification而是从桌面,我点击nottification,我得到两个版本:一个来自nottification,另一个来自桌面。在这种情况下,有没有办法关闭旧版本?