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;
|
}
|
}
|