我在打开我的应用程序的左角有通知。它工作正常。问题是,每次用户按下通知时,它会再次在内存中创建应用程序,但不会关闭旧应用程序。
当然,我可以在第一次触摸后隐藏通知图标,但我不想要。
有没有办法使用已经打开的同一个应用程序?
以下是创建此通知的服务:
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;
}
}