Intent服务,选择activity启动
Intent service, choose activity to launch
我有一个 class 实现了从 Web 服务检索一些数据的 IntentService,如果条件得到验证,则在通知栏的设备上显示通知。
这是代码:
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 activity(这是我应用程序的第一个 activity)。如果用户关闭了应用程序,这是可以的,但如果应用程序在前台,点击通知会导致应用程序重新启动。
有一种快速检查应用程序是否在前台的方法,如果是,则启动与 SplashScreen 不同的 activity?
希望这会有所帮助。您检查您的应用是否处于 运行 前台,并根据 PendingIntent
的更改意图访问 Whosebug : check android application is in foreground or not?
我有一个 class 实现了从 Web 服务检索一些数据的 IntentService,如果条件得到验证,则在通知栏的设备上显示通知。 这是代码:
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 activity(这是我应用程序的第一个 activity)。如果用户关闭了应用程序,这是可以的,但如果应用程序在前台,点击通知会导致应用程序重新启动。 有一种快速检查应用程序是否在前台的方法,如果是,则启动与 SplashScreen 不同的 activity?
希望这会有所帮助。您检查您的应用是否处于 运行 前台,并根据 PendingIntent
的更改意图访问 Whosebug : check android application is in foreground or not?