实际上我有两个关于我试图在我的应用程序中实现的通知实现的问题:
1。通知自动打开:我正在尝试在我的应用程序中的活动期间通知手机。通知正常运行的方式是,当事件被触发时,我在通知中设置的意图被调用,并显示我需要显示的屏幕。但是,通知会在状态栏上显示一秒钟然后消失。由于通知,我在后台的应用程序自动进入前台。我不希望应用程序自动进入前台。我只需要显示通知。
2。在锁定屏幕上显示通知:使用以下代码,我无法在锁定屏幕上显示通知。
这是我用来显示通知的代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("Trip Alert");
mBuilder.setContentText(StringUtils.join("You have reached ",destination.getAddress()));
mBuilder.setNumber(++numMessages);
mBuilder.setOnlyAlertOnce(true);
mBuilder.getNotification().flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
Intent detailIntent = new Intent();
detailIntent.setClass(DestinationListActivity.this,
DestinationDetailActivity.class);
detailIntent.putExtra("Destination", destination);
startActivityForResult(detailIntent, 1);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(DestinationDetailActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(detailIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
通知显示很短时间的原因是因为我在DestinationDetailsActivity onCreate
中编写了以下代码:
if (getIntent().getExtras() != null) {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(100);
..
}
但如果此代码不会出现,我该如何解除通知?
如果我需要解释更多,请告诉我。