a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
package com.basic.security.service;
 
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.text.TextUtils;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.utils.UpdateApk;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
 
public class UpgradeAppService extends Service {
    private static final String TAG = "KeepLifeService";
    private String mPackName;
    private ActivityManager mActivityManager;
    private boolean running = false;
 
    /**
     * 获取当前进程名称
     *
     * @return
     */
    public static String getProcessName() {
        try {
            File file = new File("/proc/" + Process.myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            String processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
            return processName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 进程是否存活
     *
     * @return
     */
    public static boolean isRunningProcess(ActivityManager manager, String processName) {
        if (manager == null)
            return false;
        List<ActivityManager.RunningAppProcessInfo> runnings = manager.getRunningAppProcesses();
        if (runnings != null) {
            for (ActivityManager.RunningAppProcessInfo info : runnings) {
                if (TextUtils.equals(info.processName, processName)) {
                    return true;
                }
            }
        }
        return false;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        running = true;
        mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        String process = getProcessName();
        mPackName = getPackageName();
        BaseApplication.getApplication().executorService.execute(() -> {
            while (running) {
                try {
//                        System1.out.println("UpgradeAppService.run 开始升级");
                    UpdateApk.getApk();
//                        UpdateApk.checkUpdateInLoop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                    SystemClock.sleep(60*1000);
                SystemClock.sleep(24 * 60 * 60 * 1000);
            }
        });
    }
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        running = false;
    }
}