广告屏react-native项目
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

154 lines
5.5 KiB

  1. package com.screenad;
  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.NotificationChannel;
  5. import android.app.Service;
  6. import android.content.Intent;
  7. import android.os.Build;
  8. import android.os.IBinder;
  9. import android.app.ActivityManager;
  10. import android.content.Context;
  11. import android.content.pm.PackageInfo;
  12. import android.content.pm.PackageManager;
  13. import android.util.Log;
  14. import java.util.concurrent.ExecutorService;
  15. import java.util.concurrent.Executors;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import com.screenad.MainApplication;
  19. import it.sauronsoftware.cron4j.Scheduler;
  20. /**
  21. * 守护进程,同时用来升级程序
  22. */
  23. public class DaemonService extends Service {
  24. // 核心进程 Service ID
  25. private final static int CORE_SERVICE_ID = -5122;
  26. private ExecutorService executorService = Executors.newSingleThreadExecutor();
  27. Scheduler scheduler;
  28. public DaemonService() {
  29. }
  30. private static void log(String text) {
  31. Log.i("DaemonService",text);
  32. }
  33. @Override
  34. public void onCreate() {
  35. super.onCreate();
  36. log("created");
  37. //利用 Android 漏洞提高进程优先级, 前台进程
  38. String CHANNEL_ONE_ID = "CHANNEL_ONE_ID";
  39. String CHANNEL_ONE_NAME= "CHANNEL_ONE_ID";
  40. NotificationChannel notificationChannel= null;
  41. //进行8.0的判断
  42. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  43. notificationChannel= new NotificationChannel(CHANNEL_ONE_ID,
  44. CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
  45. notificationChannel.setShowBadge(true);
  46. notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
  47. NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  48. manager.createNotificationChannel(notificationChannel);
  49. }
  50. Notification notification= new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
  51. .setContentTitle("DaemonService")
  52. .build();
  53. notification.flags|= Notification.FLAG_NO_CLEAR;
  54. startForeground(CORE_SERVICE_ID, notification);
  55. }
  56. @Override
  57. public int onStartCommand(Intent intent, int flags, int startId) {
  58. log("started");
  59. executorService.submit(new Runnable() {
  60. @Override
  61. public void run() {
  62. startCronTabTask();
  63. }
  64. });
  65. return START_STICKY;
  66. }
  67. private boolean isProcessWork(Context context, String processName) {
  68. ActivityManager myAM = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  69. List<ActivityManager.RunningAppProcessInfo> processInfos = myAM.getRunningAppProcesses();
  70. if (processInfos == null) {
  71. return false;
  72. }
  73. for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
  74. if (processInfo.processName.equals(processName)) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. private boolean isBackground(Context context, String packageName) {
  81. ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  82. List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
  83. for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
  84. if (appProcess.processName.equals(packageName)) {
  85. /* BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200 */
  86. log(ppProcess.processName + " appimportace =" + appProcess.importance);
  87. if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  88. log("处于后台" + appProcess.processName);
  89. return true;
  90. } else {
  91. log("处于前台" + appProcess.processName);
  92. return false;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * 开启cron4jtab定时任务
  100. */
  101. private void startCronTabTask() {
  102. if (scheduler != null) {
  103. scheduler.stop();
  104. }
  105. scheduler = new Scheduler();
  106. scheduler.isDaemon();
  107. scheduler.schedule("* * * * *", new Runnable() {
  108. public void run() {
  109. try {
  110. if (!isProcessWork(MainApplication.getContext(), "com.screenad") ||
  111. isBackground(MainApplication.getContext(), "com.screenad")) {
  112. log("正在重启 com.screenad...");
  113. Intent newIntent = MainApplication.getContext().getPackageManager().getLaunchIntentForPackage("com.screenad");
  114. MainApplication.getContext().startActivity(newIntent);
  115. }else{
  116. log("运行良好 com.screenad...");
  117. }
  118. } catch (Exception e) {
  119. log(e.getMessage());
  120. }
  121. }
  122. });
  123. scheduler.start();
  124. }
  125. @Override
  126. public IBinder onBind(Intent intent) {
  127. throw new UnsupportedOperationException("Not yet implemented");
  128. }
  129. @Override
  130. public void onDestroy() {
  131. super.onDestroy();
  132. }
  133. }