【发布时间】:2015-04-02 10:18:03
【问题描述】:
我有一个实现 IntentService 的类,它从 web 服务中检索一些数据,如果验证了某个条件,则在通知栏的设备上显示通知。 这是代码:
public BackgroundService() {
super("SERVICENAME");
}
@Override
protected void onHandleIntent(Intent intent) {
if (verifyConditions()) {
showNotification();
}
}
private void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.notification_icon);
// .setContentIntent(pendingIntent);
Intent notificationIntent = new Intent(this.getApplicationContext(),
SplashScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
// set sound
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setContentIntent(contentIntent);
Notification notification = builder.build();
// Hide the notification after it's selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
现在,如果用户点击通知,则会启动 SplashScreen 活动(这是我的应用程序的第一个活动)。如果用户关闭了应用程序,这没关系,但如果应用程序在前台,点击通知会导致应用程序重新启动。 有一种快速的方法可以检查应用程序是否在前台,如果是,则启动与 SplashScreen 不同的活动?
【问题讨论】:
-
请阅读
Intent.FLAG_ACTIVITY_CLEAR_TOP的文档
标签: android android-intent android-notifications