Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Activity start from notification

Tags:

android

I have problem my MainActivity can be created by 3 ways: 1) standart launch App 2) from Service 3) from notification click. How I can check when it starts from notification click?

Notification code:

private void createNotification()
{
    Log.d("service createNotification",MainActivity.TAG);
    Context context = getApplicationContext();
    Intent notificationIntent = new Intent(this,MainActivity.class);
    intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(this.getString(R.string.notification_title))
            .setContentText(this.getString(R.string.notification_text))             
            .setContentIntent(pendingIntent)             
            .setSmallIcon(R.drawable.ic_launcher);

    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification());
}
like image 974
Abbath Avatar asked Sep 01 '13 18:09

Abbath


People also ask

How can I check if an activity is already running before starting it?

Depending on what you are trying to do (update the current activity from a service?). You could just register a static listener in the service in your activity onStart method then the correct listener will be available when your service wants to update the UI.

How can I tell if an Android notification was clicked?

@Phill You can update that setting variable in the example NotificationClickedOn method, just set some ID value in alarmIntent. SetAction like "UpdateSettings" and test to see if the intent that got passed in includes that action and if so update your variable...

How do I check foreground activity?

It's very easy to detect when an Activity goes background/foreground just by listening to the lifecycle events, onStop() and onStart() of that Activity.


1 Answers

add

intent.putExtra("started_from","notification");

to the code that starts the intent from the notifications, and the same thing to the other startActivity calls just change the value, then inside your activity

String startedFrom = getIntent().getStringExtra("started_from");

for more refer to this question: How do I get extra data from intent on Android?

like image 147
Ali Alnoaimi Avatar answered Oct 06 '22 06:10

Ali Alnoaimi