|
- package com.screenad;
-
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.NotificationChannel;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Build;
- import android.os.IBinder;
- import android.app.ActivityManager;
- import android.content.Context;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
-
- import android.util.Log;
-
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import com.screenad.MainApplication;
-
- import it.sauronsoftware.cron4j.Scheduler;
-
-
- /**
- * 守护进程,同时用来升级程序
- */
- public class DaemonService extends Service {
- // 核心进程 Service ID
- private final static int CORE_SERVICE_ID = -5122;
- private ExecutorService executorService = Executors.newSingleThreadExecutor();
- Scheduler scheduler;
-
- public DaemonService() {
- }
-
- private static void log(String text) {
- Log.i("DaemonService",text);
- }
- @Override
- public void onCreate() {
- super.onCreate();
- log("created");
- //利用 Android 漏洞提高进程优先级, 前台进程
- String CHANNEL_ONE_ID = "CHANNEL_ONE_ID";
- String CHANNEL_ONE_NAME= "CHANNEL_ONE_ID";
- NotificationChannel notificationChannel= null;
- //进行8.0的判断
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- notificationChannel= new NotificationChannel(CHANNEL_ONE_ID,
- CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
- notificationChannel.setShowBadge(true);
- notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
- NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- manager.createNotificationChannel(notificationChannel);
- }
-
- Notification notification= new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
- .setContentTitle("DaemonService")
- .build();
- notification.flags|= Notification.FLAG_NO_CLEAR;
-
- startForeground(CORE_SERVICE_ID, notification);
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- log("started");
- executorService.submit(new Runnable() {
- @Override
- public void run() {
- startCronTabTask();
- }
- });
- return START_STICKY;
- }
-
- private boolean isProcessWork(Context context, String processName) {
- ActivityManager myAM = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- List<ActivityManager.RunningAppProcessInfo> processInfos = myAM.getRunningAppProcesses();
- if (processInfos == null) {
- return false;
- }
- for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
- if (processInfo.processName.equals(processName)) {
- return true;
- }
- }
- return false;
- }
-
- private boolean isBackground(Context context, String packageName) {
- ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
- for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
- if (appProcess.processName.equals(packageName)) {
- /* BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200 */
- log(ppProcess.processName + " appimportace =" + appProcess.importance);
- if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
- log("处于后台" + appProcess.processName);
- return true;
- } else {
- log("处于前台" + appProcess.processName);
- return false;
- }
- }
- }
- return false;
- }
-
-
- /**
- * 开启cron4jtab定时任务
- */
- private void startCronTabTask() {
- if (scheduler != null) {
- scheduler.stop();
- }
- scheduler = new Scheduler();
- scheduler.isDaemon();
- scheduler.schedule("* * * * *", new Runnable() {
- public void run() {
- try {
- if (!isProcessWork(MainApplication.getContext(), "com.screenad") ||
- isBackground(MainApplication.getContext(), "com.screenad")) {
- log("正在重启 com.screenad...");
- Intent newIntent = MainApplication.getContext().getPackageManager().getLaunchIntentForPackage("com.screenad");
- MainApplication.getContext().startActivity(newIntent);
- }else{
- log("运行良好 com.screenad...");
- }
- } catch (Exception e) {
- log(e.getMessage());
- }
- }
- });
- scheduler.start();
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- throw new UnsupportedOperationException("Not yet implemented");
- }
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
-
- }
|