Android - Show/Hide system bar on 4.2.2 (Nexus 10)

假如想象 提交于 2019-12-03 03:58:52

To show and hide the system bar and notification bar on 4.2.2 and others:

Hide:

    try
    {
        String command;
        command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
        Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command }, envp);
        proc.waitFor();
    }
    catch(Exception ex)
    {
        Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
    }

Show:

    try 
    {
         String command;
         command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService";
         Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command }, envp);
         proc.waitFor();
    } 
    catch (Exception e) 
    {
          e.printStackTrace();
    }

I think you should not use system calls via Runtime.exec() to get that result. You should look at the code in FullscreenActivity template (sources are placed in <android-sdk-folder>/tools/templates/activities/FullscreenActivity/root): that is a full working example showing how to show/hide system bars (both top and bottom one) programmatically, and it even supports animations for API 13+.

Bhupinder

Answer by goodm works fine, but most of us are not aware about envp

So here is full code:

HIDE

try
{
    String command;
    command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";

    ArrayList<String> envlist = new ArrayList<String>();
    Map<String, String> env = System.getenv();
    for (String envName : env.keySet()) {
        envlist.add(envName + "=" + env.get(envName));
    }
    String[] envp = (String[]) envlist.toArray(new String[0]);
    Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command }, envp);
    proc.waitFor();
}
catch(Exception ex)
{
    Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}

You can use similar for show.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!