Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:28:58+00:00 2026-06-01T08:28:58+00:00

I have a database in my application in which I store something like a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:28:59+00:00Added an answer on June 1, 2026 at 8:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database application in which a group is modeled like this: TABLE
I would like to develop an Android application which will have a local database.
I have a .net client application which is connected to a remote database. Is
I have a PHP web application which uses a MySQL database for object tagging,
What do I have to consider in database design for a new application which
I have an application A with a domain-model which is mapped to a database
i have a windows application made in VB.NET which maintains a database, and i
I have a Rails project which has a Postgres database for the actual application
I have a web application, which is using a SQL Server 2005 database. My
I have to build such application in which i have to use the database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.