java - adding a snooze button to alarm app(seekbar and pendding intent) -


i'm beginner android programmer, hope quotation ok..

i got working alarm clock tried snooze using seekbar. however, think im not using pendding intent, or understanding processed of right. cant find way check seekbar in ringtone activity(cant "r.findbyid.." ),or convey activity seekbar status in realtime. mainactivity:

public class mainactivity extends appcompatactivity {  alarmmanager alarm_manager; private static timepicker alarm_timepicker; private static textview statustxt; context context; private static seekbar snoze_sb; private pendingintent pendingintent;//used dellay intent hour user choose alarm @ @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     this.context= this;     statustxt=(textview) findviewbyid(r.id.textview) ;//initialize variables     alarm_timepicker=(timepicker) findviewbyid(r.id.timepicker);//initialize variables     alarm_manager=(alarmmanager) getsystemservice(alarm_service);//initialize variables     final intent my_intent = new intent(this.context, alarm_reciver.class);//intent alarm_reciever class     final calendar calendar = calendar.getinstance();//creat instance of calender     final togglebutton alarm_button =(togglebutton) findviewbyid(r.id.alarmstatus);     final seekbar snoze_sb=(seekbar)findviewbyid(r.id.myseek);     final boolean[] snoze_flag = {false};     snoze_flag[0]=false;     snoze_sb.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() {         @override         public void onprogresschanged(seekbar seekbar, int progress, boolean b) {             if((double)progress/(double)seekbar.getmax()>0.75)//checking user drag bar enough                 snoze_flag[0] =true;         }          @override         public void onstarttrackingtouch(seekbar seekbar) {          }          @override         public void onstoptrackingtouch(seekbar seekbar) {              seekbar.setprogress(0);//reset seekbar          }     });      alarm_button.setonclicklistener(new view.onclicklistener() {//listener- maybe should try without one.         @override         public void onclick(view view) {             if(alarm_button.ischecked())             {                 string time=get_time(calendar);//fuction gets time string                 my_intent.putextra("click_status",true);//put data in intent- tells button clicked                 my_intent.putextra("snoze", snoze_flag[0]);                 pendingintent= pendingintent.getbroadcast(mainactivity.this, 0, my_intent,pendingintent.flag_update_current);//sending broadcast                 alarm_manager.set(alarmmanager.rtc_wakeup,calendar.gettimeinmillis(),pendingintent);//tells phone set alarm                 statustxt.settext("the frindlyalarm set "+time);                 statustxt.settextcolor(color.parsecolor("#ff1aed0b"));//change color , text on text             }             else             {                 my_intent.putextra("click_status", false);//put data in intent - tells button unclicked                 alarm_manager.cancel(pendingintent);//canel old alarm user set                 sendbroadcast(my_intent);                 statustxt.settext("the frindlyalarm off");                 statustxt.settextcolor(color.parsecolor("#ffff0000"));             }          }     }); }  public string get_time(calendar calendar) {     int hours,minutes;     //getting time user chose in timepicker calender varible     if (android.os.build.version.sdk_int >= 23){// checking api choose between gethour , getcurrenthour.         calendar.set(calendar.hour_of_day,alarm_timepicker.getcurrenthour());         calendar.set(calendar.minute,alarm_timepicker.getcurrentminute());         hours=alarm_timepicker.getcurrenthour();         minutes=alarm_timepicker.getcurrentminute();     } else{         calendar.set(calendar.hour_of_day,alarm_timepicker.gethour());         calendar.set(calendar.minute,alarm_timepicker.getminute());         hours=alarm_timepicker.gethour();         minutes=alarm_timepicker.getminute();     }     string time;     if(hours>=10)//5:06-->05:06        time=string.valueof(hours)+":";     else         time="0"+string.valueof(hours)+":";     if(minutes<10)//05:6-->05:06        time=time+"0"+string.valueof(minutes);     else        time=time+string.valueof(minutes);     return time; } 

alarm_receiver:

    public class alarm_reciver extends broadcastreceiver {     @override     public void onreceive(context context, intent intent) {         log.e("in alarm","yyyyyyyyyyyyyyy");         boolean is_clicked = intent.getextras().getboolean("click_status");//getting boolean, button clicked?         boolean snoze=intent.getextras().getboolean("snoze");         intent service_intent= new intent(context, ringtoneplayingservice.class);//intent service ringtone playing         service_intent.putextra("click_status", is_clicked);//passing button status         service_intent.putextra("snoze",snoze);         context.startservice(service_intent);//start ringtone service     } } 

and ringtone_playing_service:

    public class ringtoneplayingservice extends service {      mediaplayer media_song;     private boolean isrunning;     private context context;      @nullable     @override     public ibinder onbind(intent intent) {         return null;     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         log.i("localservice", "received start id " + startid + ": " + intent);         boolean button_status = intent.getextras().getboolean("click_status");         boolean snoze=intent.getextras().getboolean("snoze");         boolean check_snoze=false;         if(!this.isrunning&&button_status) {//if theres no music , button clicked           //  notifi_function();             check_snoze=true;             media_song = mediaplayer.create(this, r.raw.madagascar_move_it);// creat instance of media player             media_song.start();//start ringtone             this.isrunning=true;         }         else if(this.isrunning&&!button_status)         {             media_song.stop();             media_song.reset();             this.isrunning=false;         }         if(snoze&&check_snoze) {             snoze_operator();            // button_status=false;         }           return start_not_sticky;     }     public void snoze_operator() {         int snoze_time = 1;         calendar calendar = calendar.getinstance();         calendar.add(calendar.minute, snoze_time);//getting time after snoze calendar         long new_alarm_time = calendar.gettimeinmillis();         //build intent , pending intent set snooze alarm         intent alarmintent = new intent(ringtoneplayingservice.this, alarm_reciver.class);         alarmmanager alarm_manager = (alarmmanager) getsystemservice(alarm_service);         pendingintent sender = pendingintent.getbroadcast(ringtoneplayingservice.this, 0, alarmintent, pendingintent.flag_update_current);         alarm_manager.set(alarmmanager.rtc_wakeup, new_alarm_time, sender);         toast.maketext(ringtoneplayingservice.this, "snoozed ass up", toast.length_short).show();     }     public void notifi_function(){         notificationmanager noti_maneger = (notificationmanager) getsystemservice(notification_service);//set notification service         intent intent_mainactivity = new intent(this.getapplicationcontext(), mainactivity.class); //intent take user mainactivity         pendingintent noti_pendint =pendingintent.getactivity(this, 0, intent_mainactivity, 0);         notification alarm_popup = null;         if (android.os.build.version.sdk_int >= android.os.build.version_codes.o) {//put right command, according api             alarm_popup = new notification.builder(this, "0").setcontenttitle("an alarm going off !")                     .setcontenttext("click me").setcontentintent(noti_pendint).setautocancel(true).build();         }         else         {             alarm_popup = new notification.builder(this).setcontenttitle("an alarm going off !")                     .setcontenttext("click me").setcontentintent(noti_pendint).setautocancel(true).build();         }         noti_maneger.notify(0, alarm_popup);//set commend calls notification       }//for reason bug system     @override     public void ondestroy() {         // tell user stopped         super.ondestroy();         toast.maketext(this, "on destroyed called", toast.length_short).show();         this.isrunning=false;     } } 

hoped used site properly, if question specify, let me know. want understand how can check snoze button in realtime, , info ringtone activity.. thank much!


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -