質問

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){}
役に立ちましたか?

解決

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);
       }
    }

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top