Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Murat Hasdemir
Front End Web Development Techdegree Graduate 20,968 PointsTimed Notification setup
Okay this thing getting over my head :S I'm trying to set series of notifications in the status bar I can make instant one and have to say it works right but I cant find a way to make it set series of timed events.
Can anyone direct me to a good tutorial?
Murat Hasdemir
Front End Web Development Techdegree Graduate 20,968 Pointsyes but daily for user specified time like user will say "remind me this for next 60 day".
2 Answers
Greg Schmidt
5,908 PointsHopefully the below will give you some Ideas. It's from a project I created. CountDownTimer starts out at 60 seconds and goes down to 0 at which point a series of activities happen. addInWordTimer continuously runs a task every 20 seconds until it is cancelled.
private void addInTimer() {
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
mTimer.setText("" + millisUntilFinished / 1000);}
public void onFinish() {
run = false;
mAddInn.setText("");
mTimer.setText("Done!");
mTopic.setText("");
soundFile = R.raw.airhorn;
playSound();
StartButton.setEnabled(true);
NewTopicButton.setEnabled(true);}
}.start();
}
private void addInWordTimer() {
final Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
if(run) {
TimerMethod();
}else {
myTimer.cancel();}}
}, 20100, 20100);
}
private void TimerMethod() {
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
soundFile = R.raw.ding;
playSound();
String newNoun = mWords.getANoun();
mAddInn.setText(newNoun);
}
};
Murat Hasdemir
Front End Web Development Techdegree Graduate 20,968 PointsThats an interesting code but if I read it right it needs to be app on run. What I'm trying to do is when you set series of notifications like don't forget to go market or check the work progress and while app not running it just sound an alarm and put a note in to the status bar of android. Have to dig alarm manager put I can't figure out it in the developer site.
Harry James
14,780 PointsBasically, what you'll want to do is check if the current date is equal to or less than the date that the notification is set to display until, like this:
if (currentDate < displayUntilDate) {
// Display notification
}
else {
// Delete the notification
}
You can do this using the Calendar class, like this:
TextView textView = (TextView) findViewById(R.id.textView);
Calendar calendar = Calendar.getInstance();
int currentDate = calendar.get(Calendar.DATE);
String currentDateAsString = Integer.toString(currentDate);
textView.setText(currentDateAsString);
The above example will get the current day and display it in a TextView, just so that you can test it :) You can also make it get the month as well if you want but, that is totally optional and depends how long the notifications are going to stay for. And, here is the developer page you are looking for :D
Hopefully this will help and, if you have any more questions, give me a shout :)
Harry James
14,780 PointsHarry James
14,780 PointsHey Murat! :)
Are you trying to make it so that the notification will have a delay after a trigger or so that it will display at a specified time (e.g: 12:30)?