0

i build a server that sends questions to clients depending on the cells they are in, for example if they are in Rome's cell, the server will send them questions different from the questions will be sent to clients in London's cell, and when a question be sent to a client, the server will make it as unavailable to the cell for one hour, i mean that question will never be sends to any client in that cell for one hour

and my question is how can i make java method for make that question is available after one hour?

EDIT1:

i have a hashtable , the keys are the cells, and the values are the question asked in these cells

EDIT2

this is the hashtable

static Hashtable<Integer, List<Integer>> unavialbeQuestions;

and when i asked question in a cell i make this

unavialbeQuestions.get(cellID).add(questionID);

and i want something like this

function makeQuestionAvailable(int questionID, int cellID){}
William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • Are you using a database for questions and clients? or what? please share some server details. –  Jun 26 '12 at 14:02
  • @djaqeel yes i am using databases to save question , cells, clients and so many details , but tell me what kind of details do you want, i am sure u don't need database diagram, – William Kinaan Jun 26 '12 at 14:04

2 Answers2

1

In java you can schedule methods to run after desired amount of time has passed using Timer class. Have a look at this example for details: http://www.ibm.com/developerworks/java/library/j-schedule/index.html

EDIT1:

Call this function fter an hour:

function makeQuestionAvailable(int questionID, int cellID){
     unavialbeQuestions.get(cellID).remove(questionID);
}

EDIT2: Example code fro scheduling:

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Timer timer = new Timer();

       timer.schedule(new ScheduledTaskWithHandeler(), 5000);

    }

    final Handler handler = new Handler() {

       public void handleMessage(Message msg) {
          Toast.makeText(getApplicationContext(), "Run!",
          Toast.LENGTH_SHORT).show();
       }
    };

    class ScheduledTaskWithHandeler extends TimerTask {

       @Override
       public void run() {
          handler.sendEmptyMessage(0);
       }
    }
Caner
  • 57,267
  • 35
  • 174
  • 180
  • i will check that link thank you , but please would you see my edit – William Kinaan Jun 26 '12 at 14:26
  • after edit2: i know how can i remove elements from hashtable, but how can i make your function starts after 1hour from putting the question in the hashtable? and thank you for answering – William Kinaan Jun 26 '12 at 14:30
  • see edit2, it should be similar to that. that code schedules a task to run 5seconds later. – Caner Jun 26 '12 at 14:33
0

When using Android generally speaking you should use the AlarmManager class rather than a timer, especially if you're doing this in the background, TimerTasks are prone to being killed by the OS.

See this post

Community
  • 1
  • 1