2015-04-02 56 views
0

我有一个实现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?

+0

plaese阅读“Intent.FLAG_ACTIVITY_CLEAR_TOP”的文档 – Selvin 2015-04-02 10:19:31

回答