首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

悬浮菜单+杜撰按键

2013-08-04 
悬浮菜单+虚拟按键1.首先控制悬浮窗要用service2.悬浮窗的参数if (wm null)wm (WindowManager) getAp

悬浮菜单+虚拟按键
1.首先控制悬浮窗要用service

2.悬浮窗的参数

        if (wm == null)            wm = (WindowManager) getApplicationContext().getSystemService("window");        if (ballWmParams == null) {            ballWmParams = new WindowManager.LayoutParams();// ((MyApplication)                                                            // getApplication()).getMywmParams();            ballWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;            ballWmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;            ballWmParams.gravity = Gravity.LEFT | Gravity.TOP;            ballWmParams.x = 50;            ballWmParams.y = 50;            ballWmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;            ballWmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;            ballWmParams.format = PixelFormat.RGBA_8888;            // 添加显示层        }        wm.addView(ballView, ballWmParams);


移除的时候要wm.removeViewImmediate(v);

3.LinearLayout的边框,用drawable类型的xml画出

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >    <gradient        android:angle="180"        android:centerX="5"        android:centerY="5"        android:endColor="#22080808"        android:gradientRadius="100"        android:startColor="#ddffffff"        android:type="radial" />    <stroke        android:dashWidth="2dp"        android:width="2dp"        android:color="#99ffffff" />    <corners android:radius="5dp" />    <!-- <solid android:color="#11ffffff" /> -->    <padding        android:bottom="1dp"        android:left="1dp"        android:right="1dp"        android:top="1dp" /></shape>



---------------------------------------------虚拟按键

分为两种思路,一种是使用系统签名+反射机制,一种是root
前一种,如果有签名的很方便,后一种可以适用大多是机型。




1.反射方法调用按键,反射+签名+特殊权限+平台编译
    <uses-permission android:name="android.permission.INJECT_EVENTS" />


void test() {        new Thread() {            public void run() {                KeyEvent keyEventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);                KeyEvent keyEventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);                final int repeatCount = (KeyEvent.FLAG_VIRTUAL_HARD_KEY & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0;                final KeyEvent evDown = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),                        KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, repeatCount, 0, KeyCharacterMap.VIRTUAL_KEYBOARD,                        0, KeyEvent.FLAG_VIRTUAL_HARD_KEY | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,                        InputDevice.SOURCE_KEYBOARD);                final KeyEvent evUp = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),                        KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK, repeatCount, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,                        KeyEvent.FLAG_VIRTUAL_HARD_KEY | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,                        InputDevice.SOURCE_KEYBOARD);                Class<?> ClassInputManager;                try {                    ClassInputManager = Class.forName("android.hardware.input.InputManager");                    Method[] methods = ClassInputManager.getMethods();                    System.out.println("cchen " + Arrays.toString(methods));                    Method methodInjectInputEvent = null;                    Method methodGetInstance = null;                    for (Method method : methods) {                        System.out.println("cchen " + method.getName());                        if (method.getName().contains("getInstance")) {                            methodGetInstance = method;                        }                        if (method.getName().contains("injectInputEvent")) {                            methodInjectInputEvent = method;                        }                    }                    Object instance = methodGetInstance.invoke(ClassInputManager, null);                    boolean bool = InputManager.class.isInstance(instance);                    System.out.println("cchen  -- " + bool);                    // methodInjectInputEvent =                    // InputManager.getMethod("injectInputEvent",                    // KeyEvent.class, Integer.class);                    methodInjectInputEvent.invoke(instance, evDown, 0);                    methodInjectInputEvent.invoke(instance, evUp, 0);                } catch (Exception e) {                    e.printStackTrace();                }            }        }.start();    }



2.root方式

获得root权限,模拟在android机器上在shell中输入命令模拟按键

代码源于这里:
http://leoly-fullnexus4.googlecode.com/svn/trunk/FullNexus4/

要导入其中的类库。

            // String apkRoot="chmod 777 "+getPackageCodePath();            // RootCommand(apkRoot);            // full();   public static boolean RootCommand(String command) {        Process process = null;        DataOutputStream os = null;        try {            process = Runtime.getRuntime().exec("su");            os = new DataOutputStream(process.getOutputStream());            os.writeBytes(command + "\n");            os.writeBytes("exit\n");            os.flush();            process.waitFor();        } catch (Exception e) {            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());            return false;        } finally {            try {                if (os != null) {                    os.close();                }                process.destroy();            } catch (Exception e) {            }        }        Log.d("*** DEBUG ***", "Root SUC ");        return true;    }    private void full() {        try {            InputShell localInputShell = getInputShell();            String str1 = "keycode " + KeyEvent.KEYCODE_BACK;            localInputShell.runCommand(str1);        } catch (Exception localException) {            String str2 = localException.getMessage();            Log.e("Button Savior", str2);            localException.printStackTrace();        }    }    public InputShell getInputShell() throws Exception {        String str1 = null;        InputStream localInputStream = null;        FileOutputStream localFileOutputStream = null;        byte[] arrayOfByte = new byte[4096];        InputShell localInputShell1 = this.mInputShell;        AssetManager localAssetManager;        File localFile;        int m;        if (localInputShell1 == null) {            localAssetManager = this.getApplicationContext().getResources().getAssets();            str1 = this.getApplicationContext().getFilesDir().getAbsolutePath();            int i = Build.VERSION.SDK_INT;            if (i > 15) {                Log.d("button", "using JB code");                String str2 = str1 + "/input2_jb.jar";                localFile = new File(str2);                str2 = "input2_jb.jar";                localInputStream = localAssetManager.open(str2);            } else if (i > 10 && i < 16) {                String str2 = str1 + "/input2_hc.jar";                localFile = new File((String) str2);                str2 = "input2_hc.jar";                localInputStream = localAssetManager.open(str2);            } else {                String str2 = str1 + "/input2.jar";                localFile = new File(str2);                str2 = "input2.jar";                localInputStream = localAssetManager.open(str2);            }            localFileOutputStream = new FileOutputStream(localFile);            m = localInputStream.read(arrayOfByte);            if (m == -1)                return null;            localFileOutputStream.write(arrayOfByte, 0, m);            localFileOutputStream.close();            localInputStream.close();            this.mInputShell = new InputShell("su", str1);        }        return this.mInputShell;    }



--------------------------------------------
整个源码上传到http://download.csdn.net/detail/ccsosnfs/5790721

热点排行