How to silence android phone based on set time -
i'm trying write program silence phone based on time. instance, press button can set start time , end time. @ start time phone silence. @ end time phone go regular ringer mode. silence phone not go regular mode once hits end time. have lot of things commented out previous attempts @ trying solve issue. uncommented parts have. help/guidance on issue.
import java.util.calendar; import android.app.activity; import android.app.dialog; import android.app.timepickerdialog; import android.content.context; import android.media.audiomanager; import android.os.bundle; import android.text.format.time; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.timepicker; import android.widget.toast; public class presetedit extends activity { private audiomanager maudiomanager; private boolean mphoneissilent; button save; private button starttime; private button endtime; private int shour; private int sminute; private int ehour; private int eminute; static final int time_dialog_id = 0; static final int time_dialog_id2 = 1; private timepickerdialog.ontimesetlistener stimesetlistener = new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int hourofday, int minute) { shour = hourofday; sminute = minute; updatedisplay(); displaytoast(); } }; private timepickerdialog.ontimesetlistener etimesetlistener = new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int hourofday, int minute) { ehour = hourofday; eminute = minute; updatedisplaye(); displaytoaste(); } }; private void updatedisplay() { starttime.settext( new stringbuilder() .append(pad(shour)).append(":") .append(pad(sminute))); } private void updatedisplaye(){ endtime.settext(new stringbuilder() .append(pad(ehour)).append(":") .append(pad(eminute))); } private void displaytoast() { toast.maketext(this, new stringbuilder().append("time choosen ").append(starttime.gettext()), toast.length_short).show(); } private void displaytoaste() { toast.maketext(this, new stringbuilder().append("time choosen ").append(endtime.gettext()), toast.length_short).show(); } private static string pad(int c) { if (c >= 10) return string.valueof(c); else return "0" + string.valueof(c); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.preset_edit); textview eventtxt = (textview)findviewbyid(r.id.txtviewevent); starttime = (button)findviewbyid(r.id.btnstarttime); endtime = (button)findviewbyid(r.id.btnendtime); starttime.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(time_dialog_id); } }); endtime.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(time_dialog_id2); } }); final calendar cal = calendar.getinstance(); shour = cal.get(calendar.hour); sminute = cal.get(calendar.minute); updatedisplay(); final calendar cale = calendar.getinstance(); calendar tmp = (calendar)cale.clone(); tmp.add(calendar.hour, 0); tmp.add(calendar.minute, 2); //ehour = cale.get(calendar.hour); calendar stoptime = tmp; //eminute = cale.get(calendar.minute); ehour = tmp.get(calendar.hour); eminute = tmp.get(calendar.minute); updatedisplaye(); //save = (button)findviewbyid(r.id.btnsave); //get event txt change according button click /*bundle car = getintent().getextras(); string receivingdata = car.getstring("eventcar"); textview eventtxt = (textview)findviewbyid(r.id.event); eventtxt.settext(receivingdata);*/ /*intent = getintent(); a.getstringextra("eventdrive");*/ string drv = getintent().getextras().getstring("event"); if(drv!=null) { eventtxt.settext(drv); time = new time(time.getcurrenttimezone()); //time end = new time(time.getcurrenttimezone()); time current = new time(time.getcurrenttimezone()); //time settoend = new time(time.getcurrenttimezone()); current.settonow(); now.settonow(); //now.hour = 6; //now.minute = 35; //string stime = ""+ now.hour+":"+now.minute; //starttime.settext(stime); //end.hour = now.hour; //end.minute = now.minute + 2; //string etime = ""+ end.hour+":"+end.minute; //endtime.settext(etime); //settoend.hour = ehour; //settoend.minute = 13; if (shour == now.hour && sminute == now.minute) { maudiomanager =(audiomanager)presetedit.this.getsystemservice(context.audio_service); maudiomanager.setringermode(audiomanager.ringer_mode_silent); //or adjust volume here instead setting silent profile ringer } if(current.hour == ehour && current.minute == eminute) { maudiomanager =(audiomanager)presetedit.this.getsystemservice(context.audio_service); maudiomanager.setringermode(audiomanager.ringer_mode_normal); } }//end if string mov = getintent().getextras().getstring("event"); if(mov!=null) eventtxt.settext(mov); string study = getintent().getextras().getstring("event"); if(study!=null) eventtxt.settext(study); string act = getintent().getextras().getstring("event"); if(act!=null) eventtxt.settext(act); /*intent intent = getintent(); string text = intent.getstringextra("eventdriving");*/ //eventtxt.settext(text); } /*private void setbuttonclicklistener() { save = (button)findviewbyid(r.id.btnsave); save.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (mphoneissilent) { //change normal mode maudiomanager.setringermode(audiomanager.ringer_mode_normal); mphoneissilent = false; save.settext("activate"); } else { //change silent mode maudiomanager.setringermode(audiomanager.ringer_mode_silent); //maudiomanager.setringermode(audiomanager.ringer_mode_vibrate); mphoneissilent = true; save.settext("activated"); } // toggle ui again } }); } private void checkifphoneissilent() { int ringermode = maudiomanager.getringermode(); if (ringermode == audiomanager.ringer_mode_silent) { mphoneissilent = true; } else { mphoneissilent = false; } } @override protected void onresume() { super.onresume(); checkifphoneissilent(); }*/ @override protected dialog oncreatedialog(int id) { switch (id) { case time_dialog_id: return new timepickerdialog(this, stimesetlistener, shour, sminute, false); case time_dialog_id2: return new timepickerdialog(this, etimesetlistener, ehour, eminute, true); } return null; } }
read service - serve. problem lies in fact aktiviti not exists @ time, how turn sound on.
Comments
Post a Comment