android - How to invoke an activity from background services when arrives a notification? -
i want check notifications background receivers or services. notification shown, should invoke activity.
mainacticityclass
here have created alarm class call broadcast manager @ specific interval
public class mainactivity extends appcompatactivity { private context context; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); this.context = this; intent alarm = new intent(this.context, alarmreceiver.class); boolean alarmrunning = (pendingintent.getbroadcast(this.context, 0, alarm, pendingintent.flag_no_create) != null); if(alarmrunning == false) { pendingintent pendingintent = pendingintent.getbroadcast(this.context, 0, alarm, 0); alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); alarmmanager.setrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime(), 60000, pendingintent); } }
alarm receiver class
this broadcast class invoke ground
public class alarmreceiver extends broadcastreceiver { public alarmreceiver() { } @override public void onreceive(context context, intent intent) { intent background = new intent(context, mylistenerservices.class); context.startservice(background); } }
mylistener
this subclass of notificationlistener services reads incoming notification unable read notification inactive class integrate class read kind of incoming notification background
public class mylistenerservices extends notificationlistenerservice{ public mylistenerservices() { } private boolean isrunning; private context context; private thread backgroundthread; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { this.context = this; this.isrunning = false; this.backgroundthread = new thread(mytask); } private runnable mytask = new runnable() { public void run() { // here log.d("msg", "servicerunning"); statusbarnotification[] statusbarnotifications = getactivenotifications(); log.d("msg", "new object2 "+statusbarnotificationsarray); if (statusbarnotifications.length > 0) { log.d("msg", "new object "+statusbarnotifications.length); // intent = new intent(context, automaticcameraactivity.class); i.setflags(intent.flag_activity_new_task); startactivity(i); // } // }catch (exception e){ // log.d("msg",e.getmessage()); } stopself(); } }; @override public void onnotificationposted(statusbarnotification sbn) { notification mnotification=sbn.getnotification(); log.v("msg"," notification"+ mnotification); } @override public void ondestroy() { this.isrunning = false; } @override public int onstartcommand(intent intent, int flags, int startid) { if(!this.isrunning) { this.isrunning = true; this.backgroundthread.start(); } return start_sticky; } }
any appreciated
thanks in advance
create pending intent
intent resultintent = new intent(this, resultactivity.class); //change resultactivity activity want invoke ... // because clicking notification opens new ("special") activity, there's // no need create artificial stack. pendingintent resultpendingintent = pendingintent.getactivity( this, 0, resultintent, pendingintent.flag_update_current );
more information in create notification
Comments
Post a Comment