Reputation: 951
I have a database in my application in which I store something like a reminder. One of the columns is a String representation of time at which the reminder should be "reminded", like this: hh:mm. I create a Thread in my main activity to monitor all the reminders in regular intervals and check if its time to set of the alarm. Before I creeate this thread, I load times + IDs of all database rows into an ArrayList and I work with this ArrayList in my thread instead of the database itself (I had some problems doing that). Anyway, here is the code:
First, I declare global variables using an Application class:
public class MyApplication extends Application {
public ArrayList<String> reminders = new ArrayList<String>();
public int hour;
public int minute;
}
And in my main activity:
public class Home extends Activity {
ArrayList<String> reminders;
String time:
int hour;
int minute;
@Override
public void onCreate(Bundle savedInstanceState) {
//The usual code at the beginning of onCreate method
//I load my global variables
MyApplication appState = ((MyApplication)getApplicationContext());
reminders = appState.reminders;
hour = appState.hour;
minute = appState.minute;
//I save curren time into global variables
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
//I loop over all rows of database and save what I need from them into
//Strings in ArrayList reminders. I do this only once on Application
//launch to load already existing rows. When the application runs
//I can always add or remove existing rows using special Activity
//I create and start my Thread
Thread t = new Thread() {
try {
while (true) {
time = hour + ":" + minute;
if (reminders.size() > 0) {
for (int i = 0; i < reminders.size(); i++) {
if (reminders.get(i).contains(time)) {
//One of the Strings in ArrayList reminders
//contains String representation of current
//time (along with the ID of database row).
//Here I will probably be starting a new
//Activity
}
}
}
minute++;
if (minute == 60) {
minute = 0;
hour++;
}
if (hour == 24) {
hour = 0;
}
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t.start();
}
}
It seems to be working fine, but Im really uncomfortable with this solution. My first question is if there is any way to improve this code? Could I just create my variables int hour, int minute and ArrayList reminders in the Thread itself and than load contents of reminders just before the Thread loop sequence? This way I wouldnt have to use Application class to store the variables, but I need them to be global, the Thread needs to be running even when I start new Activityes in my application and it needs to store those variables correctly.
My second question i if there is some completly different way to approach this that you would recommand.
Thanks very much!
Id like to add something to my question. Since Ill be using an AlarmManager, Im going to need to set repeating events in more than just one Activity. So my question is, should I use different instances of AlarmManager in each Activity to add or remove events, or should I use the same instance that would be declared globaly? Thanks.
Upvotes: 1
Views: 435
Reputation: 7102
AlarmManager is a better choice for executing a task periodically, such as checking a table for "reminders" and alerting the user if necessary. The reason is that the Thread will not run when the CPU goes to sleep. You need a WakeLock if you want your thread to stay awake, and that will be expensive on power. AlarmManager has optimizations for this.
Second, you don't need global vars for any of this. So please don't extend Application. It's not required.
Upvotes: 3