3ae7ffc5
孙向锦
project init
|
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
60
61
62
63
64
65
|
package com.sunvote.xpadapp.server;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import com.jaredrummler.android.processes.AndroidProcesses;
import com.sunvote.util.LogUtil;
import com.sunvote.xpadapp.MainActivity;
public class MoniService extends Service {
private Handler handler ;
public MoniService() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(handler == null){
handler = new Handler();
}
if(handler != null){
handler.removeCallbacks(checkTask);
handler.post(checkTask);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
private Runnable checkTask = new Runnable() {
@Override
public void run() {
boolean ret = AndroidProcesses.isMyProcessInTheForeground();
LogUtil.i("MoniService","checkResult:" + ret);
if(!ret){
Intent intent = new Intent(MoniService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
startActivity(intent);
}
if(handler != null){
handler.removeCallbacks(checkTask);
handler.postDelayed(checkTask,1000);
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(checkTask);
}
}
|