java - Notification action button dosen't stop the alarm ringtone -


i set alarm , want disable ongoing alarm using notification action button without opening activity.

i have attached whole code below.

when user set alarm call broadcast receiver , them call ringtone service start ringtone. , fire notification. in notification have disable button. have done resource had found on internet.

disable button on notification didn't work. fault

ontoggled button start alarm main activity

public void ontoggleclicked(view view) {     long time;     if (((togglebutton) view).ischecked()) {         toast.maketext(mainactivity.this, "alarm on", toast.length_short).show();         calendar calendar = calendar.getinstance();           if (build.version.sdk_int >= 23) {             calendar.set(calendar.hour_of_day, alarmtimepicker.gethour());             calendar.set(calendar.minute, alarmtimepicker.getminute());         } else {             calendar.set(calendar.hour_of_day, alarmtimepicker.getcurrenthour());             calendar.set(calendar.minute, alarmtimepicker.getcurrentminute());         }          intent = new intent(this, alarmreceiver.class);         pendingintent = pendingintent.getbroadcast(this, 0, intent, 0);         alarmmanager = (alarmmanager) getsystemservice(alarm_service);         // setrepeating() lets specify precise custom interval--in case,         // 1 minutes.         alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), 1000 * 60, pendingintent);       } else {         alarmmanager.cancel(pendingintent);         toast.maketext(mainactivity.this, "alarm off", toast.length_short).show();     } } 

my broadcast receiver class

public class alarmreceiver extends broadcastreceiver { public static ringtone ringtone;  @override public void onreceive(context context, intent intent) {     toast.maketext(context, "alarm! wake up! wake up!", toast.length_long).show();       // using service class     intent = new intent(context, ringtoneplayingservice.class);     context.startservice(i);      createnotification(context); }  public void createnotification(context context) {       notificationcompat.builder builder = new notificationcompat.builder(context)             .setsmallicon(android.r.drawable.ic_dialog_alert)             .setcontenttitle("it prayer time")             .setcontenttext("prayer")             .setsmallicon(r.mipmap.ic_launcher)             .setsubtext("tab cancel ringtone")             .setpriority(notificationcompat.priority_high);       //to add dismiss button     intent dismissintent = new intent(context, ringtoneplayingservice.class);     dismissintent.setaction(ringtoneplayingservice.action_dismiss);      pendingintent pendingintent = pendingintent.getservice(context,             123, dismissintent, pendingintent.flag_update_current);     notificationcompat.action action = new notificationcompat.action             (android.r.drawable.ic_lock_idle_alarm, "dismiss", pendingintent);     builder.addaction(action);     // end of setting action button notification       intent intent1 = new intent(context, mainactivity.class);     pendingintent pintent = pendingintent.getactivity(context, 123, intent1,             pendingintent.flag_update_current);     builder.setcontentintent(pintent);       notificationmanager notificationmanager = (notificationmanager)             context.getsystemservice(notification_service);     notification notification = builder.build();     notification.flags |= notification.flag_auto_cancel;     notificationmanager.notify(123, notification);   } 

my ringtoneplaying service class

public class ringtoneplayingservice extends service{ private static final string tag = ringtoneplayingservice.class.getsimplename(); private static final string uri_base = ringtoneplayingservice.class.getname() + "."; public static final string action_dismiss = uri_base + "action_dismiss";  private ringtone ringtone;  @override public ibinder onbind(intent intent) {     return null; }  @override public int onstartcommand(intent intent, int flags, int startid) {     log.d(tag, "onstartcommand");      if(intent == null) {         log.d(tag, "the intent null.");         return start_redeliver_intent;     }      uri alarmuri = ringtonemanager.getdefaulturi(ringtonemanager.type_alarm);     if (alarmuri == null) {         alarmuri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);     }     ringtone = ringtonemanager.getringtone(this, alarmuri);     ringtone.play();       string action = intent.getaction();      if(action_dismiss.equals(action))         dismissringtone();       return start_not_sticky; }  public void dismissringtone() {     intent = new intent(this, ringtoneplayingservice.class);     stopservice(i); }  @override public void ondestroy() {     ringtone.stop(); } 

that fault

when click on disable on notification again call receiver class that's why ringtone keeps playing. keep modifying , works fine.

public class ringtoneplayingservice extends service { private static final string tag = ringtoneplayingservice.class.getsimplename(); private static final string uri_base = ringtoneplayingservice.class.getname() + "."; public static final string action_dismiss = uri_base + "action_dismiss";   private ringtone ringtone;  @override public ibinder onbind(intent intent) {     return null; }  @override public int onstartcommand(intent intent, int flags, int startid) {     log.d(tag, "onstartcommand");      if (intent == null) {         log.d(tag, "the intent null.");         return start_redeliver_intent;     }      string action = intent.getaction();      if (action_dismiss.equals(action))         dismissringtone();     else {         uri alarmuri = ringtonemanager.getdefaulturi(ringtonemanager.type_alarm);         if (alarmuri == null) {             alarmuri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);         }         ringtone = ringtonemanager.getringtone(this, alarmuri);         ringtone.play();     }      return start_not_sticky; }  public void dismissringtone() {     // stop alarm rigntone     intent = new intent(this, ringtoneplayingservice.class);     stopservice(i);      // dismiss alarm ring again or trigger again     alarmmanager amanager = (alarmmanager) getsystemservice(alarm_service);     intent intent = new intent(getbasecontext(), alarmreceiver.class);     pendingintent pendingintent = pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_cancel_current);     amanager.cancel(pendingintent);      // canceling current notification     notificationmanager notificationmanager =             (notificationmanager)getsystemservice(getapplicationcontext().notification_service);     notificationmanager.cancel(321); }  @override public void ondestroy() {     ringtone.stop(); }} 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -