MonitorService.java
4.61 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.lotus.town.ali;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import com.lotus.town.R;
import com.lotus.town.config.SupportAction;
import com.lotus.town.notify.NotificationUtils;
import com.lotus.town.notify.PushModel;
import com.sdk.Sdk;
import java.util.List;
public class MonitorService extends Service implements MediaPlayer.OnPreparedListener {
private static boolean isAlive = false;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
isAlive = true;
}
public static boolean isApplicationBroughtToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
initNotification();
}
monitor();
stop = true;
return START_STICKY;
}
// Handler handler = new Handler(){
// @Override
// public void handleMessage(Message msg) {
// Toast.makeText(MonitorService.this,"handler",Toast.LENGTH_SHORT).show();
// monitorScreenOn();
// handler.sendEmptyMessageDelayed(0,10000);
// }
// };
private boolean stop = false;
private void monitor(){
final long timeInterval = 10000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
if(stop){
break;
}
monitorScreenOn();
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
private void monitorScreenOn(){
if (((PowerManager) this.getSystemService(Context.POWER_SERVICE)).isScreenOn()) {
} else {
if(!Sdk.isInSplash) {
Intent i = new Intent();
i.setPackage(this.getPackageName());
i.setAction(SupportAction.SCREEN_OFF_MY);
this.sendBroadcast(i);
stop = true;
}
}
}
public static void invoke(Context context){
try {
// if (!isAlive) {
Intent i = new Intent();
i.setClass(context, MonitorService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
} else {
context.startService(i);
}
// }
} catch (Exception e){
e.printStackTrace();
}
}
public static void invokeMp3(Context context,boolean isStart){
try {
Intent i = new Intent();
i.setClass(context, MonitorService.class);
i.putExtra("m_p",isStart?1:2);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
} else {
context.startService(i);
}
} catch (Exception e){
e.printStackTrace();
}
}
private void initNotification() {
PushModel pushModel = new PushModel();
pushModel.setContent("荷花小镇持续赚钱中");
pushModel.setId("111");
pushModel.setTitle("荷花小镇");
pushModel.setFlag(Notification.FLAG_ONGOING_EVENT|Notification.FLAG_FOREGROUND_SERVICE);
NotificationUtils.init().createNotificationChannel(this);
Notification notification = NotificationUtils.init().sendNotify(this, pushModel, "0", "0");
startForeground(111,notification);
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
}