How to define and use a system property in Android Instrumentation test?

前端 未结 7 1038
时光说笑
时光说笑 2020-12-02 23:25

I am trying to use some arguments for an Instrumentation test. I noticed that I can read system properties with System.getProperty() function. So I use setprop

7条回答
  •  长情又很酷
    2020-12-03 00:11

    Based on provided answer, Slightly modified version of SetProperty

        public void setSystemProperty(String Key, String value){
        InputStreamReader in = null;
        BufferedReader reader = null;
        try {
            Process proc = Runtime.getRuntime().exec("/system/bin/setprop "+Key+" "+value);
            in = new InputStreamReader(proc.getInputStream());
            reader = new BufferedReader(in);
    
            String line = null;
            Log.d("Saurabh Shell" ,"");
            while ( (line = reader.readLine()) != null)
                Log.d("Shell" , line);
            Log.d("Saurabh Shell", "");
            int exitVal = proc.waitFor();
            Log.d("Saurabh Shell","Process exitValue: " + exitVal);
    
        } catch (IOException e) {
           e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            closeQuietly(in);
            closeQuietly(reader);
        }
    }
    

    close Input and reader

        public  void closeQuietly(Closeable closeable) {
        if (closeable == null) return;
        try {
            closeable.close();
        } catch (IOException ignored) {
        }
    }
    

提交回复
热议问题