ANDROID 隐藏 任务栏 systemui systembar 全屏显示

来源:互联网 发布:我国教育经费机制知 编辑:程序博客网 时间:2024/04/20 03:03

原文:http://blog.csdn.net/xiaogezq0/article/details/8662464


(1)开始为了隐藏systemui利用过 kill com.android.systemui线程进行的隐藏,但是总有一个com.android.systemui.SystemUIService进行启动

                          我开始还是比较的坏的就弄了一个监听每500毫秒进行检测一次进行查杀

 

代码:

[java] view plaincopyprint?
  1. @Override  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.main);  
  5.          ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);// 获得activity管理  
  6.   
  7.         List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();  
  8.         for (RunningAppProcessInfo runningAppProcessInfo : infos) {  
  9.             System.out.println("processName:====================:"+runningAppProcessInfo.processName);  
  10.             if(runningAppProcessInfo.processName.equals("com.android.systemui")){  
  11.                 System.out.println("processpid: "+runningAppProcessInfo.pid);  
  12.                     String str = "/system/bin/kill "+runningAppProcessInfo.pid;  
  13.                     System.out.println("str:  "+str);  
  14.                     Process process;  
  15.                     Runtime runtime;  
  16.                     try {  
  17.                         runtime = Runtime.getRuntime();  
  18.                         process = runtime.exec("su");  
  19.                         System.out.println("01010101010");  
  20.                         process = runtime.exec(str);  
  21.                          int exitVal = process.waitFor();  
  22.                         System.out.println("66666666666666666666666");  
  23.                         break;  
  24.                     } catch (IOException e) {  
  25.                         System.out.println(e);  
  26.                     } catch (InterruptedException e) {  
  27.                         // TODO Auto-generated catch block  
  28.                         e.printStackTrace();  
  29.                     }  


 

(2)通过长时间研究我研究到了SystemUI.apk,我就就想对这个东西进行操作了。开始我删除掉后,systeui还是运行着,我就用kill命令直接杀掉这个线程,然后就开始报错了。说找不到SystemUI什么的。及其的烦人,不过重新启动就可以了。就没有那个错误了。

苍天真的不负有心人,本人找到一个更好的方法,原来大概是这样的:通过命令移除SystemUI.apk放到一个文件夹中,然后重新启动com.systemui.SystemUIService这个服务

就可以了。如果想恢复就把SystemUI.apk移到/system/app/下并且重新启动com.systemui.SystemUIService这个服务

代码参照:

[java] view plaincopyprint?
  1. File systemUIapkFile = new File("/system/app/SystemUI.apk");  
  2.      
  3.    @Override  
  4.    public void onCreate(Bundle savedInstanceState) {  
  5.        super.onCreate(savedInstanceState);  
  6.        setContentView(R.layout.main);  
  7.          
  8.        final ToggleButton systemBarToggleButton = (ToggleButton) findViewById(R.id.systemBarToggleButton);  
  9.        systemBarToggleButton.setChecked(systemUIapkFile.exists());  
  10.        systemBarToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  11.            @Override  
  12.            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  13.                systemBarToggleButton.setChecked(isChecked);  
  14.                switchSystemUI();  
  15.                if (isChecked) {  
  16.                    Intent intent = new Intent();  
  17.                    intent.setComponent(new ComponentName(  
  18.                            "com.android.systemui",  
  19.                            "com.android.systemui.SystemUIService"));  
  20.                    startService(intent);  
  21.                }  
  22.            }  
  23.        });  
  24.    }  
  25.      
  26.    private void switchSystemUI() {  
  27.        try {  
  28.            Process p;  
  29.            p = Runtime.getRuntime().exec("su");  
  30.   
  31.            // Attempt to write a file to a root-only  
  32.            DataOutputStream os = new DataOutputStream(p.getOutputStream());  
  33.            os.writeBytes("mount -o remount,rw /dev/block/stl6 /system\n");  
  34.            if (systemUIapkFile.exists()) {  
  35.                os.writeBytes("mv /system/app/SystemUI.apk /system/SystemUI.apk\n");  
  36.            }else {  
  37.                os.writeBytes("mv /system/SystemUI.apk /system/app/SystemUI.apk\n");  
  38.            }  
  39.            os.writeBytes("mount -o remount,ro /dev/block/stl6 /system\n");  
  40.   
  41.            // Close the terminal  
  42.            os.writeBytes("exit\n");  
  43.            os.flush();  
  44.            p.waitFor();  
  45.        } catch (Exception e) {  
  46.            ShowErrorGlobal(e);  
  47.        }  
  48.    }  
  49.      
  50.    protected void ShowErrorGlobal(Exception e) {  
  51.        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  52.        PrintStream stream = new PrintStream(baos);  
  53.        e.printStackTrace(stream);  
  54.        stream.flush();  
  55.   
  56.        new AlertDialog.Builder(this)  
  57.                .setIconAttribute(android.R.attr.alertDialogIcon)  
  58.                .setTitle("Epic fail")  
  59.                .setMessage("Error: " + new String(baos.toByteArray())).show();  
  60.    }  


(3)

这种更牛逼,什么还自己通过命令操作。都是也路子。人家google给咱提供的有接口直接用就行啊

直接代码参考吧:

[java] view plaincopyprint?
  1. int flag = context.getWindow().getDecorView().getSystemUiVisibility();  
  2. //      int fullScreen = View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN;  
  3.         int fullScreen = 0x8;  
  4.         if(visible) {  
  5.             if((flag & fullScreen) != 0) {  
  6.                 context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);  
  7.             }  
  8.         } else {  
  9.             if((flag & fullScreen) == 0) {  
  10.                 context.getWindow().getDecorView().setSystemUiVisibility(flag | fullScreen);  
  11.             }  
  12.         } 



补充:

网上搜罗了相应的资料,好像4.1有个SYSTEM_UI_FLAG_HIDE_NAVIGATION可以处理System Bar的隐藏,不管我在代码里面如何设置,就是不见效果,怀疑是手机上面的功能块吧,没得4.1的实体手机,就不具体深究。网上有些工具软件对其处理,有的需要Root权限,有的只是移动SystemUI软件包位置作消显,感觉不实用,查看View.java的源码,发现还有个标志位SYSTEM_UI_FLAG_SHOW_FULLSCREEN,实践了一会,预期的效果就出来了。

关键代码:

/** * 设置系统栏可见性 */public static void setSystemBarVisible(final Activity context,boolean visible) {    int flag = context.getWindow().getDecorView().getSystemUiVisibility();   // 获取当前SystemUI显示状态    // int fullScreen = View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN;    int fullScreen = 0x8;   // 4.1 View.java的源码里面隐藏的常量SYSTEM_UI_FLAG_SHOW_FULLSCREEN,其实Eclipse里面也可以调用系统隐藏接口,重新提取下android.jar,这里就不述了。    if(visible) {   // 显示系统栏        if((flag & fullScreen) != 0) {  // flag标志位中已经拥有全屏标志SYSTEM_UI_FLAG_SHOW_FULLSCREEN            context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);   // 显示系统栏        }    } else {    // 隐藏系统栏        if((flag & fullScreen) == 0) {  // flag标志位中不存在全屏标志SYSTEM_UI_FLAG_SHOW_FULLSCREEN            context.getWindow().getDecorView().setSystemUiVisibility(flag | fullScreen); // 把全屏标志位加进去        }    }}

这里多加了判断的方法,供调用

/** * 判断状态栏是否显示 */public static boolean isSystemBarVisible(final Activity context) {    int flag = context.getWindow().getDecorView().getSystemUiVisibility();    // return (flag & View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN) != 0;     return (flag & 0x8) == 0;}

代码可能对3.0或4.0以上的手机版本不适用,只实测4.1平板,在4.04的小米手机上无效果

补充:最近看了下官方4.1的源码,唉,代码不一致,无效果,原来是Rockchip厂商新加入的功能,无奈,但是此功能可移植成功,抱歉。

原创粉丝点击