giozh
giozh

Reputation: 10068

Intent service, choose activity to launch

I have a class that implements IntentService that retrieve some data from a webservice, and if a condition is verified, show notification on notification bar's device. This is the code:

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);
}

Now if user tap on notification, SplashScreen activity is launched (it's first activity of my app). this is ok if user has close the application, but if application is in foreground, tapping on notification cause restart of app. There's a fast way to check if app is in foreground, and if yes, launch a different activity thant SplashScreen?

Upvotes: 0

Views: 408

Answers (1)

Bharatesh
Bharatesh

Reputation: 9009

Hope this will help. You check your app is running foreground or not and according to that change intent for PendingIntent visit StackOverFlow : check android application is in foreground or not?

Upvotes: 1

Related Questions