Self updating app

后端 未结 3 1205
青春惊慌失措
青春惊慌失措 2021-01-30 03:12

TL:DR; version ;)

  • my app should run without user interaction (autostart etc works)

  • it should update itself (via apk) without any user interactio

3条回答
  •  遇见更好的自我
    2021-01-30 04:10

    If su -c doesn't work, try su 0 (only rooted devices can do su!)

    The full answer looks like this:

    private void installNewApk()
        {
            String path = mContext.getFilesDir().getAbsolutePath() + "/" + LOCAL_FILENAME;
            mQuickLog.logD("Install at: " + path);
            ProcessUtils.runProcessNoException(mQuickLog, "su", "0", "pm", "install", "-r", path);
        }
    

    With this class defined:

    public class ProcessUtils {
        Process process;
        int errCode;
        public ProcessUtils(String ...command) throws IOException, InterruptedException{
            ProcessBuilder pb = new ProcessBuilder(command);
            this.process = pb.start();
            this.errCode = this.process.waitFor();
        }
    
        public int getErrCode() {
            return errCode;
        }
    
        public String getOutput() throws IOException {
            InputStream inputStream = process.getInputStream();
            InputStream errStream = process.getErrorStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line + System.getProperty("line.separator"));
            }
    
            br = new BufferedReader(new InputStreamReader(errStream));
            while ((line = br.readLine()) != null) {
                sb.append(line + System.getProperty("line.separator"));
            }
            return sb.toString();
        }
    
    
        public static String runProcess(String ...command) throws IOException, InterruptedException {
            ProcessUtils p = new ProcessUtils(command);
            if (p.getErrCode() != 0) {
                // err
            }
            return p.getOutput();
        }
    
        public static void runProcessNoException(String ...command) {
            try {
                runProcess(command);
            } catch (InterruptedException | IOException e) {
                // err
            }
        }
    }
    

提交回复
热议问题