SystemUtils 系统级调用类

public class SystemUtils {

    private static final ComponentName SYSTEM_UI = new ComponentName("com.android.systemui",
            "com.android.systemui.SystemUIService");
    private static final String SU_PATH1 = "/system/bin/su";
    private static final String SU_PATH2 = "/system/xbin/su";

    private SystemUtils() {

    }

    private static String getSuPath() {
        File file = new File(SU_PATH1);
        if (file.exists() && file.isFile() && file.canExecute()) {
            return SU_PATH1;
        } else {
            File file1 = new File(SU_PATH2);
            if (file1.exists() && file1.isFile() && file1.canExecute())
                return SU_PATH2;
        }
        return null;
    }

    public static final boolean isSystemUIRunning() {
        ActivityManager actvityManager = (ActivityManager) EBookDroidApp.context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> rsiList = actvityManager.getRunningServices(1000);

        for (RunningServiceInfo rsi : rsiList) {
            if (SYSTEM_UI.equals(rsi.service)) {
                return true;
            }
        }
        return false;
    }

    public static void startSystemUI() {
        if (isSystemUIRunning()) {
            return;
        }
        exec(new String[] { "/system/bin/am", "startservice", "-n", "com.android.systemui/.SystemUIService" });
    }

    public static boolean stopSystemUI() {
        if (!isSystemUIRunning()) {
            return true;
        }

        final String su = getSuPath();
        if (su == null) {
            return false;
        } else {
            exec(new String[] { su, "-c", "service call activity 79 s16 com.android.systemui" });
        }
        return true;
    }

    public static void toggleSystemUI() {
        if (isSystemUIRunning()) {
            stopSystemUI();
        } else {
            startSystemUI();
        }
    }

    public static void exec(final String... as) {
        (new Thread(new Runnable() {

            public void run() {
                execImpl(as);
            }
        })).start();
    }

    private static String execImpl(String... as) {
        try {
            Process process = Runtime.getRuntime().exec(as);
            InputStreamReader r = new InputStreamReader(process.getInputStream());
            StringWriter w = new StringWriter();
            char ac[] = new char[8192];
            int i = 0;
            do {
                i = r.read(ac, 0, 8192);
                w.write(ac, 0, i);
            } while (i != -1);
            r.close();
            process.waitFor();
            return w.toString();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值