Skip to content

Commit f22ffc1

Browse files
Fix service things
1 parent 1604d3d commit f22ffc1

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class Example extends Activity {
7575
- 🟩 Looper
7676
- 🟨 Activity
7777
- 🟨 Read bitmap.
78-
- 🟥 Services.
78+
- 🟨 Services.
7979
- 🟥 Write bitmap.
8080

8181
### Events

src/br/nullexcept/mux/app/Activity.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ public void setContentView(View view){
4747
mWindow.setContentView(view);
4848
}
4949

50-
protected void post(Runnable runnable){
51-
Looper.getMainLooper().post(runnable);
50+
protected void post(Runnable runnable) {
51+
postDelayed(runnable, 1);
52+
}
53+
54+
protected void postDelayed(Runnable runnable, int time){
55+
Looper.getMainLooper().postDelayed(()->{
56+
if(isRunning()) {
57+
runnable.run();
58+
}
59+
}, time);
5260
}
5361

5462
protected void setTitle(String title){

src/br/nullexcept/mux/app/Application.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public static void initialize(Valuable<Activity> creator){
3838
TexelAPI.destroy();
3939
glfwTerminate();
4040
System.gc();
41-
System.exit(0);
41+
new Thread(()->{
42+
try {
43+
Thread.sleep(2000);
44+
} catch (InterruptedException e) {}
45+
System.exit(0);
46+
}).start();
4247
}
4348

4449
private static final void loop(){
@@ -91,6 +96,8 @@ static void boot(Window window, Activity activity) {
9196
}
9297

9398
public static void stop(){
99+
for (Service service: services.values())
100+
service.myLooper.stop();
94101
Looper.getMainLooper().stop();
95102
}
96103

@@ -105,8 +112,8 @@ public static <T extends Service> T beginService(Class<T> clazz, Valuable<T> ser
105112

106113
services.put(name, sv);
107114
new Thread(()->{
108-
looper.post(sv::onCreate);
109115
looper.initialize();
116+
looper.post(sv::onCreate);
110117
looper.loop();
111118
services.remove(name);
112119
sv.onDestroy();

src/br/nullexcept/mux/app/Service.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ protected void onCreate() {
77

88
}
99

10+
protected void post(Runnable runnable) {
11+
postDelayed(runnable, 1);
12+
}
13+
14+
protected void postDelayed(Runnable run, int time) {
15+
myLooper.postDelayed(run, time);
16+
}
17+
1018
protected void finish() {
1119
myLooper.stop();
1220
}

src/br/nullexcept/mux/utils/Log.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.PrintStream;
5+
import java.util.Date;
56

67
public class Log {
7-
private static final long start = System.currentTimeMillis();
8+
private static final Date date = new Date();
89

910
private static final String time(){
10-
long time = (System.currentTimeMillis() - start)/1000;
11-
return String.format("%02d:%02d", (time/60), time % 60);
11+
date.setTime(System.currentTimeMillis());
12+
return String.format("%02d:%02d", date.getHours(), date.getMinutes());
1213
}
1314

1415
private static String toString(Object obj){

0 commit comments

Comments
 (0)