-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyAsync.java
59 lines (42 loc) · 1.22 KB
/
TinyAsync.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
53
54
55
56
57
58
59
package com.example.tinyasync;
import java.lang.ref.WeakReference;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
public class TinyAsync<ActClass extends Activity> implements Runnable {
protected WeakReference<ActClass> weact = null;
// Runnable with activity reference
public class Actable implements Runnable {
protected ActClass act_ref = null;
@Override public void run() {
if(weact != null) {
act_ref = weact.get();
if(act_ref != null) {
actask();
}
}
}
// Override this class, has access to act_ref
public void actask() { return; }
}
public TinyAsync(WeakReference<ActClass> wref) {
this.weact = wref;
}
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
main();
}
// Run activity runnable if possible
public void act(Actable a) {
(new Handler(Looper.getMainLooper())).post(a);
}
// Start the thread that will run this task
public void start_task() {
(new Thread(this)).start();
}
// Override these in subclasses
// Main program, run in separate thread, can block on api requests etc
public void main() { return; }
}