/
RandomEventsService.java
52 lines (42 loc) · 1.43 KB
/
RandomEventsService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.bootstragram.demo.services;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.bootstragram.demo.bean.RandomSingleton;
public class RandomEventsService extends IntentService {
private final static String TAG = RandomEventsService.class.getSimpleName();
public RandomEventsService() {
super(RandomEventsService.class.getSimpleName());
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent");
while (true) {
final RandomSingleton singleton = RandomSingleton.getInstance();
singleton.increment();
long endTime = System.currentTimeMillis() + 2 * 1000;
Log.d(TAG, "Waking up at " + endTime);
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
Log.e(TAG, "Couldn't sleep in peace", e);
}
}
singleton.increment();
Log.d(TAG, "Woke up with " + singleton);
}
}
}
}