如何在通知中按pendingIntent时使用open活动

时间:2015-10-13 21:29:23

标签: android android-service android-notifications android-pendingintent

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

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

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

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

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

}

1 个答案:

答案 0 :(得分:0)

在清单中,将主要活动的启动模式设置为singleTop。看看docs。当您再次启动活动时,将在onNewIntent()方法

上收到意图