Unable to get alert within time and location range in android -
i new android , developing 1st application location based reminder adding tasks locations , date , time when reach should notified. have switch button location if set on whenever reach location should notified , if set off alert on location , date , time. can either give location alert or location , date , time alert.
layout :-
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/relative1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bckgrnd_4"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="task name" android:id="@+id/textview5" android:layout_alignbottom="@+id/taskname" android:layout_alignstart="@+id/textview6" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/taskname" android:hint="@string/hint_name" android:layout_margintop="37dp" android:layout_alignparenttop="true" android:layout_alignparentend="true" android:layout_toendof="@+id/textview7" > <requestfocus /> </edittext> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="set" android:id="@+id/button5" android:onclick="set_loc" style="@style/btnstylebeige" android:layout_margintop="37dp" android:layout_below="@+id/textview5" android:layout_alignstart="@+id/switch1"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="set location" android:id="@+id/textview6" android:layout_alignbottom="@+id/button5" android:layout_tostartof="@+id/taskname" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="always alert me" android:id="@+id/textview7" android:layout_centervertical="true" android:layout_alignparentstart="true" android:layout_marginleft="10dp"/> <switch android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/btnstylebeige" android:id="@+id/switch1" android:layout_aligntop="@+id/textview7" android:layout_alignend="@+id/relative3" /> <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/relative2" android:layout_margintop="37dp" android:layout_below="@+id/switch1" android:layout_alignparentstart="true"> <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pickdate" android:src="@drawable/cal" style="@style/btnstylebeige" android:layout_marginstart="42dp" android:layout_alignparenttop="true" android:layout_alignparentstart="true" /> <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/picktime" style="@style/btnstylebeige" android:src="@drawable/alert" android:layout_marginstart="67dp" android:layout_alignbottom="@+id/pickdate" android:layout_toendof="@+id/pickdate" /> </relativelayout> <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/relative3" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="10dp"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="add task" android:onclick="add_task" android:drawableleft="@drawable/add" style="@style/btnstylebeige" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="cancel" android:onclick="cancel" android:drawableleft="@drawable/bck" style="@style/btnstylebeige" android:layout_alignparenttop="true" android:layout_toendof="@+id/button2" android:layout_marginstart="37dp" /> </relativelayout>
activity :-
public class addtasks extends activity implements compoundbutton.oncheckedchangelistener{ taskform activityform = new taskform(); private imagebutton ppickdate; private int pyear; private int pmonth; private int pday; public string date; public string loc; public double lat; public double lon; private imagebutton ppicktime; private int mhour; private int mminute; public string time; private timepickerdialog timepickerdialog; private datepickerdialog datepickerdialog; date selecteddate; /** integer uniquely define dialog used displaying date picker.*/ static final int date_dialog_id = 0; static final int time_dialog_id = 1; /** callback received when user "picks" date in dialog */ private datepickerdialog.ondatesetlistener pdatesetlistener = new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { pyear = year; pmonth = monthofyear; pday = dayofmonth; updatedate(); displaydatetoast(); } }; private timepickerdialog.ontimesetlistener mtimelistener = new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int hour, int minute) { mhour = hour; mminute = minute; updatetime(); displaytimetoast(); } }; /** updates date in textview */ private void updatedate() { /** pdisplaydate.settext( */ date = new stringbuilder() // month 0 based add 1 .append(pday).append("/") .append(pmonth + 1).append("/") .append(pyear).append(" ").tostring(); // date = pdisplaydate.gettext().tostring(); } private void updatetime() { /** pdisplaytime.settext( */ time = new stringbuilder() .append(mhour).append(":") .append(mminute).append("").tostring(); } /** displays notification when date updated */ private void displaydatetoast() { //toast.maketext(this, new stringbuilder().append("date choosen ").append(date), toast.length_long).show(); activityform.settaskdateinput(date); } private void displaytimetoast() { // toast.maketext(this, new stringbuilder().append("time choosen ").append(time), toast.length_long).show(); activityform.settasktimeinput(time); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { relativelayout relativelayout2 = (relativelayout)findviewbyid(r.id.relative2); if(ischecked) { relativelayout2.setvisibility(view.gone); } else { relativelayout2.setvisibility(view.visible); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.addtask); getactionbar().setdisplayhomeasupenabled(true); switch aswitch = (switch) findviewbyid(r.id.switch1); relativelayout relativelayout1 = (relativelayout)findviewbyid(r.id.relative1); relativelayout relativelayout3 = (relativelayout)findviewbyid(r.id.relative3); aswitch.setoncheckedchangelistener(this); init(); /** capture our view elements */ //pdisplaydate = (textview) findviewbyid(r.id.displaydate); ppickdate = (imagebutton) findviewbyid(r.id.pickdate); /** listener click event of button */ ppickdate.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(date_dialog_id); } }); /** capture our view elements */ //pdisplaytime = (textview) findviewbyid(r.id.displaytime); ppicktime = (imagebutton) findviewbyid(r.id.picktime); /** listener click event of button */ ppicktime.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(time_dialog_id); } }); /** current date , time */ final calendar cal = calendar.getinstance(); pyear = cal.get(calendar.year); pmonth = cal.get(calendar.month); pday = cal.get(calendar.day_of_month); mhour = cal.get(calendar.hour_of_day); mminute = cal.get(calendar.minute); } /** create new dialog date , time picker */ @override protected dialog oncreatedialog(int id) { switch (id) { case date_dialog_id: datepickerdialog = new datepickerdialog(this, pdatesetlistener, pyear, pmonth, pday); date dt = new date(); calendar calendar = calendar.getinstance(); calendar.settime(dt); mhour = calendar.get(calendar.hour_of_day); mminute = calendar.get(calendar.minute); datepickerdialog.getdatepicker().setmindate(dt.gettime() - 10000); // datepickerdialog.getdatepicker().setmindate(new date().gettime() - 10000); return datepickerdialog; case time_dialog_id: timepickerdialog = new timepickerdialog(this, mtimelistener, mhour,mminute,true) ; return timepickerdialog; } return null; } public void set_loc(view v) { intent = new intent(this, set_location.class); startactivityforresult(i, 2); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == 2) { try { lat = data.getdoubleextra("lat", 0.0); lon = data.getdoubleextra("lon", 0.0); string loc_name = data.getstringextra("loc"); //toast.maketext(getapplicationcontext(), "loc " + lat + ":" + lon, // toast.length_long).show(); // loc = string.valueof(lat) + string.valueof(lon); loc = loc_name; // toast.maketext(getapplicationcontext(),"loc : " + loc_name,toast.length_long).show(); activityform.settasklocinput(loc); activityform.settasklat(lat); activityform.settasklon(lon); } catch(nullpointerexception e) { log.e("null", "null"); } } } private void persisttask(task task) { fileoutputstream fileoutputstream; try { fileoutputstream = openfileoutput("" + task.hashcode(), context.mode_private); objectoutputstream objectoutputstream = new objectoutputstream( fileoutputstream); objectoutputstream.writeobject(task); objectoutputstream.close(); fileoutputstream.close(); } catch (exception e) { log.e("file out", "file out"); } } @override public void onbackpressed() { // todo auto-generated method stub super.onbackpressed(); } public void add_task(view view) { if(activityform.gettasknameinput().gettext() != null ) { if (activityform.gettasklocinput() != null && activityform.gettasklat() != null && activityform.gettasklon() != null) { task task = new task(); task.settasknamestring(activityform.gettasknameinput().gettext() .tostring()); task.settasklocstring(activityform.gettasklocinput()); task.settasklat(activityform.gettasklat()); task.settasklon(activityform.gettasklon()); task.setdatestring(activityform.gettaskdateinput()); task.settimestring(activityform.gettasktimeinput()); persisttask(task); // toast.maketext(getapplicationcontext(), "saved=" + task.hashcode(), // toast.length_long).show(); activityform.gettasknameinput().settext(" "); calendar c = calendar.getinstance(); if (timepickerdialog != null) { timepickerdialog.updatetime(c.get(calendar.hour_of_day), c.get(calendar.minute)); } if (datepickerdialog != null) { datepickerdialog.updatedate(c.get(calendar.year), c.get(calendar.month), c.get(calendar.day_of_month)); } } else { alertdialog.builder dialog = new alertdialog.builder(this); dialog.settitle("attention"); dialog.setmessage("please select location"); dialog.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); dialog.show(); } } else { alertdialog.builder dialog = new alertdialog.builder(this); dialog.settitle("attention"); dialog.setmessage("please enter task name"); dialog.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); dialog.show(); } } private void init() { activityform .settasknameinput((edittext) findviewbyid(r.id.taskname)); }
service :-
public class locservice extends service { locationmanager lm ; location location ; // default 0.0 double lat; double lon; string name, loca; @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } @override public int onstartcommand(intent intent, int flags, int startid) { toast.maketext(getapplicationcontext(), "service wokeup", toast.length_short).show(); if(mainactivity.context != null) { lm = (locationmanager) getsystemservice(context.location_service); location = lm.getlastknownlocation(locationmanager.network_provider); } if(location != null) { lat = location.getlatitude(); lon = location.getlongitude(); log.i("",""+ lat + " | " + lon); } processtasks(); location = null; lat = 0.0; lon = 0.0; return super.onstartcommand(intent, flags, startid); } private void processtasks() { list<task> tasks = loadfiles(); if(tasks == null || tasks.size() < 1){ // toast.maketext(getapplicationcontext(),"no tasks found",toast.length_long).show(); } (task task : tasks) { if (tasks != null && task.alert) { log.i("task ",task.tostring()); //toast.maketext(getapplicationcontext(),"your task " + task.tostring(),toast.length_long).show(); // if in time range boolean timerange = true; // if in loc range boolean locrange = true; if(task.gettasklocstring() != null && task.gettasklat() !=null && task.gettasklon()!= null) { float dst = 0.0f; if (lat > 0.0 && lon > 0.0) { float[] results = new float[1]; location.distancebetween(lat, lon, task.gettasklat(), task.gettasklon(), results); float distanceinmeters = results[0]; dst = distanceinmeters; log.i("dist", "" + distanceinmeters); boolean iswithin5km = distanceinmeters < 5000; locrange = iswithin5km; } log.i("bool", "" + locrange); // toast.maketext(this,"dist " + dst ,toast.length_long).show(); if (dst < 5000) { // toast.maketext(getapplicationcontext(),"booooom",toast.length_long).show(); locrange = true; } else { locrange = false; //toast.maketext(getapplicationcontext(),"faaalse",toast.length_long).show(); } } // toast.maketext(getapplicationcontext(),"date " + task.getdatestring(),toast.length_long).show(); if(task.getdatestring() != null && task.gettimestring() != null) { // format date string datearr[] = task.getdatestring().split("/"); int day = integer.parseint(datearr[0]); int month = integer.parseint(datearr[1]); month--; int year = integer.parseint(datearr[2].trim()); // toast.maketext(getapplicationcontext(),"date " + datearr,toast.length_long).show(); // toast.maketext(getapplicationcontext(),"time " + task.gettimestring(),toast.length_long).show(); //format time string timearr[] = task.gettimestring().split(":"); if (timearr != null) { log.i("=>", timearr.tostring()); //toast.maketext(getapplicationcontext(),"time " + timearr.tostring(),toast.length_long).show(); } else { log.e("tme null", "time null"); // toast.maketext(getapplicationcontext(),"time null",toast.length_long).show(); } int hour = integer.parseint(timearr[0]); int minute = integer.parseint(timearr[1]); // month range 0-11 calendar event = calendar.getinstance(); event.set(year, month, day, hour, minute); calendar = calendar.getinstance(); log.i("event", "" + event.tostring()); log.i("now", "" + now.tostring()); int hournow = now.get(calendar.hour_of_day); int dnow = now.get(calendar.day_of_month); int mnow = now.get(calendar.month); int ynow = now.get(calendar.year); int minnow = now.get(calendar.minute); log.i("year", "" + ynow + " y " + year); log.i("mnth", "" + mnow + " m " + month); log.i("day", "" + dnow + " d " + day); log.i("minute", "" + minnow + "min" + minute); if (ynow == year && mnow == month && dnow == day) { if (hournow == hour && minnow == minute) { timerange = true; } else { log.i("hour", "" + (hour - hournow)); } } } // toast.maketext(getapplicationcontext(),"values :- " + day + " , " + month + " , " + year + " , " + hour + " , " + minute,toast.length_long).show(); // timerange = true; // toast.maketext(getapplication(),"time range " + timerange,toast.length_long).show(); // toast.maketext(getapplicationcontext(),"loc range " + locrange,toast.length_long).show(); name = task.gettasknamestring(); loca = task.gettasklocstring(); if (timerange && locrange) { toast.maketext(getapplicationcontext(),"your task " + task.tostring(),toast.length_long).show(); // alertview.task_static = task; gson gson = new gson(); string l = gson.tojson(loca); string n = gson.tojson(name); intent intent = new intent(this, alertview.class); intent.putextra("loc", l); intent.putextra("name",n); pendingintent pintent = pendingintent.getactivity(this, 0, intent, pendingintent.flag_update_current); notificationcompat.builder builder = new notificationcompat.builder( getapplicationcontext()); builder.setsmallicon(r.drawable.ic_launcher); builder.setcontenttitle("alert !!!!"); builder.setcontenttext(" " + name); builder.setlights(color.white, 1500, 1500); builder.setdefaults(notification.default_vibrate); builder.setdefaults(notification.default_lights); builder.setdefaults(notification.default_sound); builder.setdefaults(notification.flag_show_lights); builder.setcontentintent(pintent); builder.setautocancel(true); notificationmanager manager = (notificationmanager) getsystemservice(context.notification_service); manager.notify(99, builder.build()); task.alert = false; persisttask(task); } } } } private void persisttask(task task) { fileoutputstream fileoutputstream; try { fileoutputstream = openfileoutput("" + task.hashcode(), context.mode_private); objectoutputstream objectoutputstream = new objectoutputstream( fileoutputstream); objectoutputstream.writeobject(task); objectoutputstream.close(); fileoutputstream.close(); } catch (exception e) { log.e("file out", "file out"); } } private list<task> loadfiles() { list<task> tasks = new arraylist<task>(); file file = getfilesdir(); file[] files = file.listfiles(); (file temp : files) { try { objectinputstream = new objectinputstream( new fileinputstream(temp)); task task = (task) is.readobject(); is.close(); tasks.add(task); } catch (streamcorruptedexception e) { log.e("file read", "file read"); } catch (filenotfoundexception e) { log.e("file read", "file read"); } catch (ioexception e) { log.e("file read", "file read"); } catch (classnotfoundexception e) { log.e("file read", "file read"); } } return tasks; }
}
in above service first m checking if task's location not null set locrange true , if date time not null timerange true i.e here :-
boolean timerange = true; // if in loc range boolean locrange = true; if(task.gettasklocstring() != null && task.gettasklat() !=null && task.gettasklon()!= null) { float dst = 0.0f; if (lat > 0.0 && lon > 0.0) { float[] results = new float[1]; location.distancebetween(lat, lon, task.gettasklat(), task.gettasklon(), results); float distanceinmeters = results[0]; dst = distanceinmeters; log.i("dist", "" + distanceinmeters); boolean iswithin5km = distanceinmeters < 5000; locrange = iswithin5km; } log.i("bool", "" + locrange); // toast.maketext(this,"dist " + dst ,toast.length_long).show(); if (dst < 5000) { // toast.maketext(getapplicationcontext(),"booooom",toast.length_long).show(); locrange = true; } else { locrange = false; //toast.maketext(getapplicationcontext(),"faaalse",toast.length_long).show(); } } // toast.maketext(getapplicationcontext(),"date " + task.getdatestring(),toast.length_long).show(); if(task.getdatestring() != null && task.gettimestring() != null) { // format date string datearr[] = task.getdatestring().split("/"); int day = integer.parseint(datearr[0]); int month = integer.parseint(datearr[1]); month--; int year = integer.parseint(datearr[2].trim()); // toast.maketext(getapplicationcontext(),"date " + datearr,toast.length_long).show(); // toast.maketext(getapplicationcontext(),"time " + task.gettimestring(),toast.length_long).show(); //format time string timearr[] = task.gettimestring().split(":"); if (timearr != null) { log.i("=>", timearr.tostring()); //toast.maketext(getapplicationcontext(),"time " + timearr.tostring(),toast.length_long).show(); } else { log.e("tme null", "time null"); // toast.maketext(getapplicationcontext(),"time null",toast.length_long).show(); } int hour = integer.parseint(timearr[0]); int minute = integer.parseint(timearr[1]); // month range 0-11 calendar event = calendar.getinstance(); event.set(year, month, day, hour, minute); calendar = calendar.getinstance(); log.i("event", "" + event.tostring()); log.i("now", "" + now.tostring()); int hournow = now.get(calendar.hour_of_day); int dnow = now.get(calendar.day_of_month); int mnow = now.get(calendar.month); int ynow = now.get(calendar.year); int minnow = now.get(calendar.minute); log.i("year", "" + ynow + " y " + year); log.i("mnth", "" + mnow + " m " + month); log.i("day", "" + dnow + " d " + day); log.i("minute", "" + minnow + "min" + minute); if (ynow == year && mnow == month && dnow == day) { if (hournow == hour && minnow == minute) { timerange = true; } else { log.i("hour", "" + (hour - hournow)); } } }
after doing above if add 3 tasks :-
- task1 :- location >5km , no date time (for m not getting alert right)
- task2 :- location<5km , no date time (m getting alert right)
- task3 :- loaction<5km, date - 31/5/2015 , time - 11:12 (for task not checking timerange condition i.e minute add task @ 11:05 m getting alert)
what missing ? please me main part of application..
Comments
Post a Comment