get property by name from shell - Android java.util

Android examples for java.util:Properties

Description

get property by name from shell

Demo Code


//package com.java2s;

import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
    public static String getprop(String name) {
        ProcessBuilder pb = new ProcessBuilder("/system/bin/getprop", name);
        pb.redirectErrorStream(true);/*ww  w.  j  ava  2  s.  co  m*/

        Process p = null;
        InputStream is = null;
        try {
            p = pb.start();
            is = p.getInputStream();
            Scanner scan = new Scanner(is);
            scan.useDelimiter("\n");
            String prop = scan.next();
            if (prop.length() == 0)
                return null;
            return prop;
        } catch (NoSuchElementException e) {
            return null;
        } catch (Exception e) {
            // e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }
        return null;
    }
}

Related Tutorials