get User List from Shell - Android Android OS

Android examples for Android OS:Shell

Description

get User List from Shell

Demo Code


//package com.java2s;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
    public static String[] getUserList() {
        Process p;//from www .  j av  a 2 s . c  o  m
        try {
            p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            os.writeBytes("pm list-users" + "\n");
            os.writeBytes("exit\n");
            ArrayList<String> users = new ArrayList<String>();
            String test;
            bf.readLine();
            while ((test = bf.readLine()) != null) {
                users.add(test);
            }

            String[] userList = (String[]) users.toArray(new String[users
                    .size()]);

            os.flush();
            return userList;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials