java - Status bar notification click event Android for background foreground app state -


on click of status bar notification. want check if app in foreground state notification cancel or if app in background state open app. have managed app state using code in application class

public static boolean isactivityvisible() {     return activityvisible; }  public static void activityresumed() {     activityvisible = true; }  public static void activitypaused() {     activityvisible = false; }  private static boolean activityvisible; 

and putting these activities

@override protected void onresume() {   super.onresume();   myapplication.activityresumed(); }  @override protected void onpause() {   super.onpause();   myapplication.activitypaused(); } 

for notification using code

notificationmanager notificationmanager = (notificationmanager) context                 .getsystemservice(context.notification_service);          notification notification = new notification.builder(context)                 .setcontenttitle(title).setcontenttext(message)                 .setsmallicon(r.drawable.ic_on).build();          intent notificationintent = new intent(context,                 appstartactivty.class);         notificationintent.setflags(intent.flag_activity_clear_top                 | intent.flag_activity_single_top);         pendingintent intent = pendingintent.getactivity(context, 0,                 notificationintent, 0);         notification.flags |= notification.flag_auto_cancel;          notification.defaults |= notification.default_sound;          notification.defaults |= notification.default_vibrate;         notificationmanager.notify(0, notification); 

where shod put condition check app state , notification click behaviour. sorry bad inglish

you can check whether app in background/foreground using below code.

activitymanager activitymanager = (activitymanager)this.getsystemservice(context.activity_service);             arraylist<activitymanager.runningappprocessinfo> appinfo = (arraylist)activitymanager.getrunningappprocesses();             (activitymanager.runningappprocessinfo app : appinfo) {                 log.d(tag, "app: " + app.processname + "    state: " + (app.importance == 100 ? "foreground" : "background"));                 if (app.processname.equals(yourapp().getpackagename())) {                     log.d(tag, app.processname);                     if (app.importance == activitymanager.runningappprocessinfo.importance_foreground) {                        // cancel notification                     }                     break;                 }             } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -