-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeepAlive.cs
487 lines (409 loc) · 15 KB
/
KeepAlive.cs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
using Android.App;
using Android.App.Job;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Text;
using Android.Util;
using Android.Views;
using AndroidX.Core.App;
using AndroidX.Work;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace DCMS.Client.Droid
{
/// <summary>
/// 使用 ShinyAndroidApplication 接替 Application
/// </summary>
[Application(LargeHeap = true)]
public class MainApplication : ShinyAndroidApplication<Startup>
{
private static MainApplication instance;
public static Activity activity;
public static MainApplication GetInstance()
{
return instance;
}
public MainApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
instance = this;
}
public override void OnCreate()
{
base.OnCreate();
//1像素广播注册
KeepManager.GetInstance().RegisterKeep(this);
//前台服务保活
StartService(new Intent(this, typeof(ForegroundService)));
//开启粘性服务进行保活
StartService(new Intent(this, typeof(StickyService)));
//使用JobScheduler进行保活
AliveJobService.StartJob(this);
//开启保活工作
StartKeepWork();
}
private static readonly string TAG = "KeepLiveWork";
private static readonly string TAG_KEEP_WORK = "KeepLiveWork";
public void StartKeepWork()
{
WorkManager.GetInstance(this).CancelAllWorkByTag(TAG_KEEP_WORK);
Log.Debug(TAG, "keep-> dowork startKeepWork");
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(typeof(KeepLiveWork))
.SetBackoffCriteria(AndroidX.Work.BackoffPolicy.Linear, 5, Java.Util.Concurrent.TimeUnit.Seconds)
.AddTag(TAG_KEEP_WORK)
.Build();
WorkManager.GetInstance(this).Enqueue(oneTimeWorkRequest);
}
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
e.SetObserved();
var newExc = new System.Exception("TaskSchedulerOnUnobservedTaskException", e.Exception);
FileUtils.WriteLog(newExc);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var newExc = new System.Exception("CurrentDomainOnUnhandledException", e.ExceptionObject as System.Exception);
FileUtils.WriteLog(newExc);
}
private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
{
var newExc = new System.Exception("UnhandledExceptionRaiser", e.Exception);
FileUtils.WriteLog(newExc);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
AndroidEnvironment.UnhandledExceptionRaiser -= AndroidEnvironment_UnhandledExceptionRaiser;
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
}
}
#region workmanager
/// <summary>
/// 利用jetpack中的workManager启动WorkJobService进行保活
/// </summary>
public class KeepLiveWork : Worker
{
private Context _context;
public KeepLiveWork(Context context, WorkerParameters workerParams) : base(context, workerParams)
{
_context = context;
}
public override Result DoWork()
{
try
{
//启动job服务
WorkJobService.StartJob(_context);
//启动相互绑定的服务
//StartKeepService();
return new Result.Retry();
}
catch (Exception)
{
return new Result.Failure();
}
}
}
public class WorkJobService : JobService
{
private static readonly string TAG = "WorkJobService";
public static void StartJob(Context context)
{
Log.Error(TAG, "startJob");
JobScheduler jobScheduler = (JobScheduler)context.GetSystemService(Context.JobSchedulerService);
JobInfo.Builder builder = new JobInfo.Builder(8, new ComponentName(context.PackageName,
typeof(WorkJobService).Name)).SetPersisted(true);
// 小于7.0
if (Build.VERSION.SdkInt < BuildVersionCodes.N)
{
builder.SetPeriodic(1000);
}
else
{
builder.SetMinimumLatency(1000);
}
jobScheduler.Schedule(builder.Build());
}
public override bool OnStartJob(JobParameters jobParameters)
{
Log.Error(TAG, "onStartJob");
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
StartJob(this);
}
return false;
}
public override bool OnStopJob(JobParameters jobParameters)
{
//base.OnStopJob(jobParameters);
return false;
}
}
#endregion
#region jobscheduler
public class AliveJobService : JobService
{
private static readonly string TAG = "AliveJobService";
public static void StartJob(Context context)
{
JobScheduler jobScheduler = (JobScheduler)context.GetSystemService(Context.JobSchedulerService);
//setPersisted 在设备重启依然执行
// 需要增加权限 RECEIVE_BOOT_COMPLETED
JobInfo.Builder builder = new JobInfo.Builder(8, new ComponentName(context.PackageName,
typeof(AliveJobService).Name)).SetPersisted(true);
// 小于7.0
if (Build.VERSION.SdkInt < BuildVersionCodes.N)
{
// 每隔 1s 执行一次 job
// 版本23 开始 进行了改进,最小周期为 5s
builder.SetPeriodic(1000);
}
else
{
// 延迟执行任务
builder.SetMinimumLatency(1000);
}
jobScheduler.Schedule(builder.Build());
}
public override bool OnStartJob(JobParameters jobParameters)
{
Log.Error(TAG, "onStartJob");
// 如果7.0以上 轮询
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
StartJob(this);
}
return false;
}
public override bool OnStopJob(JobParameters jobParameters)
{
return false;
}
}
#endregion
#region service
/// <summary>
/// 使用粘性服务进行保活
/// </summary>
[Service]
public class StickyService : Service
{
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
return base.OnStartCommand(intent, flags, startId);
//return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
/// <summary>
/// 不同版本有差异,需分开处理 此种方法适用于音乐播放器保活,8.0以后会在通知栏显示
/// </summary>
[Service]
public class ForegroundService : Service
{
private static readonly string TAG = "ForegroundService";
private static readonly int SERVICE_ID = 1;
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnCreate()
{
base.OnCreate();
Log.Error(TAG, "ForegroundService 服务创建了");
if (Build.VERSION.SdkInt < BuildVersionCodes.JellyBeanMr2)
{
//4.3以下
//将service设置成前台服务,并且不显示通知栏消息
StartForeground(SERVICE_ID, new Notification());
}
else if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
//Android4.3-->Android7.0
//将service设置成前台服务
StartForeground(SERVICE_ID, new Notification());
//删除通知栏消息
StartService(new Intent(this, typeof(InnerService)));
}
else
{
// 8.0 及以上
//通知栏消息需要设置channel
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
//NotificationManager.IMPORTANCE_MIN 通知栏消息的重要级别 最低,不让弹出
//IMPORTANCE_MIN 前台时,在阴影区能看到,后台时 阴影区不消失,增加显示 IMPORTANCE_NONE时 一样的提示
//IMPORTANCE_NONE app在前台没有通知显示,后台时有
NotificationChannel channel = new NotificationChannel("channel", "xx", NotificationImportance.None);
if (manager != null)
{
manager.CreateNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, "channel").Build();
//将service设置成前台服务,8.x退到后台会显示通知栏消息,9.0会立刻显示通知栏消息
StartForeground(SERVICE_ID, notification);
}
}
}
/// <summary>
/// 内联服务
/// </summary>
public class InnerService : Service
{
public override void OnCreate()
{
base.OnCreate();
Log.Error(TAG, "InnerService 服务创建了");
// 让服务变成前台服务
StartForeground(SERVICE_ID, new Notification());
// 关闭自己
StopSelf();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
#endregion
#region activity
/// <summary>
/// 用于保活的1像素activity
/// </summary>
public class AliveActivity : Activity
{
private static readonly string TAG = "AliveActivity";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Log.Debug(TAG, "AliveActivity启动");
Window window = this.Window;
window.SetGravity(GravityFlags.Start | GravityFlags.Top);
WindowManagerLayoutParams @params = window.Attributes;
//宽高
@params.Width = 1;
@params.Height = 1;
//设置位置
@params.X = 0;
@params.Y = 0;
window.Attributes = @params;
KeepManager.GetInstance().SetKeep(this);
}
protected override void OnDestroy()
{
base.OnDestroy();
Log.Debug(TAG, "AliveActivity关闭");
}
}
/// <summary>
/// 息屏广播监听
/// </summary>
public class KeepAliveReceiver : BroadcastReceiver
{
private static readonly string TAG = "KeepAliveReceiver";
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
Log.Debug(TAG, "onReceive:" + action);
if (TextUtils.Equals(action, Intent.ActionScreenOff))
{
//息屏 开启
KeepManager.GetInstance().StartKeep(context);
}
else if (TextUtils.Equals(action, Intent.ActionScreenOn))
{
//开屏 关闭
KeepManager.GetInstance().FinishKeep();
}
}
}
/// <summary>
/// 1像素activity保活管理类
/// </summary>
public class KeepManager
{
private static readonly KeepManager mInstance = new KeepManager();
private KeepAliveReceiver mKeepAliveReceiver;
private WeakReference<Activity> mKeepActivity;
public KeepManager()
{
}
public static KeepManager GetInstance()
{
return mInstance;
}
/// <summary>
/// 注册 开屏 关屏 广播
/// </summary>
/// <param name="context"></param>
public void RegisterKeep(Context context)
{
IntentFilter filter = new IntentFilter();
filter.AddAction(Intent.ActionScreenOn);
filter.AddAction(Intent.ActionScreenOff);
mKeepAliveReceiver = new KeepAliveReceiver();
context.RegisterReceiver(mKeepAliveReceiver, filter);
}
/// <summary>
/// 注销 广播接收者
/// </summary>
/// <param name="context"></param>
public void UnregisterKeep(Context context)
{
if (mKeepAliveReceiver != null)
{
context.UnregisterReceiver(mKeepAliveReceiver);
}
}
/// <summary>
/// 开启1像素Activity
/// </summary>
/// <param name="context"></param>
public void StartKeep(Context context)
{
Intent intent = new Intent(context, typeof(AliveActivity));
// 结合 taskAffinity 一起使用 在指定栈中创建这个activity
intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(intent);
}
/// <summary>
/// 关闭1像素Activity
/// </summary>
public void FinishKeep()
{
if (mKeepActivity != null)
{
mKeepActivity.TryGetTarget(out Activity activity);
if (activity != null)
{
activity.Finish();
}
mKeepActivity = null;
}
}
/// <summary>
/// 设置弱引用
/// </summary>
/// <param name="keep"></param>
public void SetKeep(AliveActivity keep)
{
mKeepActivity = new WeakReference<Activity>(keep);
}
}
#endregion
}