模拟电源按钮preSS显示关机对话框 [英] Simulating power button press to display switch off dialog box

查看:562
本文介绍了模拟电源按钮preSS显示关机对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用关闭对话框中切换时,我们preSS电源按钮时出现。但我想从一个Android应用程序或JUnit测试的情况下完成这一任务。我会选择在这种情况下,最可行的办法。我一直在想这样做,但没能成功。我尝试使用以下五种方法:

第一种方法:

 长eventTime = SystemClock.uptimeMillis();布尔
                  dispateched = launcherButtonWInst.dispatchKeyEvent(新
                  KeyEvent的(eventTime,eventTime,KeyEvent.ACTION_DOWN,
                  KeyEvent.KEY code_POWER,0,0,0,0,0));
                  Log.i(LOG_TAG,将String.valueOf(dispateched));布尔
                  dispateched2 = launcherButtonWInst.dispatchKeyEvent(新
                  的KeyEvent(eventTime + 2000,eventTime + 2000,KeyEvent.ACTION_UP,
                  KeyEvent.KEY code_POWER,0,0,0,0,0));
                  Log.i(LOG_TAG,将String.valueOf(dispateched2));
 

=============================================== ========

第二apporach:

 私人无效generateKeys(){
        //获取窗口管理系统服务界面
        的IBinder wmbinder = ServiceManager.getService(窗口);
        Log.d(LOG_TAG,窗口管理器:+ wmbinder);
        IWindowManager WM = IWindowManager.Stub.asInterface(wmbinder);
        keyUpDown(WM,KeyEvent.KEY code_POWER);
    }

    私人无效keyUpDown(IWindowManager WM,INT关键code){
        尝试 {
            Log.d(LOG_TAG的keyDown:+键code);
            wm.injectKeyEvent(新的KeyEvent(KeyEvent.ACTION_DOWN,关键code),TRUE);
            Log.d(LOG_TAG,KEYUP:+键code);
            wm.injectKeyEvent(新的KeyEvent(KeyEvent.ACTION_UP,关键code),TRUE);
        }赶上(RemoteException的E){
            // TODO自动生成的catch块
            Log.e(ERROR !!!!!,e.toString());
            e.printStackTrace();
        }
    }
 

=============================================== =============

第三种方法:

 仪器仪表研究所=新规范();
        inst.sendKeyDownUpSync(KeyEvent.KEY code_POWER);
 

=============================================== =======

第四种方法:

 尝试{
                         长今= SystemClock.uptimeMillis();
                         的keyEvent下来=新的KeyEvent(现在,现在,
                         KeyEvent.ACTION_DOWN,KeyEvent.KEY code_POWER,0);

                         Log.d(LOG_TAG,down.toString());
                         的keyEvent起来=新的KeyEvent(现+ 2000年,现+ 2000,
                         KeyEvent.ACTION_UP,KeyEvent.KEY code_POWER,0);
                         Log.d(LOG_TAG,up.toString());
                         (IWindowManager.Stub.asInterface(ServiceManager.getService(窗口)))。injectKeyEvent(下降,
                         真正);
                         尝试 {
                         视频下载(2000);
                         }赶上(InterruptedException异常E){
                         // TODO自动生成的catch块
                         e.printStackTrace();
                         }
                         (IWindowManager.Stub.asInterface(ServiceManager.getService(窗口)))。injectKeyEvent(上,
                         真正);
                         }赶上(RemoteException的E){
                         Log.d(LOGTAG
                         SendKeyEvent异常:+ e.getMessage());
 

}

第五种方法:

 尝试{
                    上下文mContext = getBaseContext();
                    对话对话框=新的对话框(mContext);
                    。dialog.getWindow()的setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    ShutdownThread.shutdown(mContext,真正的);

                }赶上(例外五){
                    Log.e(ERROR !!!,e.toString());
                    e.printStackTrace();
                }
 

=============================================== ====================

但他们都不是为我工作。第五个方法是调用ShutdownThread类的shutdown()方法。但它给了我下面的错误:

 十月7号至5号:18:21.489:W / System.err的(709):android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl $ W @ 4104d798  - 权限被拒绝此窗口类型
十月七日至5日:18:21.499:W / System.err的(709):在android.view.ViewRootImpl.setView(ViewRootImpl.java:537)
十月七日至5日:18:21.499:W / System.err的(709):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
十月七日至5日:18:21.499:W / System.err的(709):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
十月七日至5日:18:21.499:W / System.err的(709):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:140)
十月七日至5日:18:21.499:W / System.err的(709):在android.app.Dialog.show(Dialog.java:278)
十月七日至5日:18:21.499:W / System.err的(709):在com.android.internal.app.ShutdownThread.shutdown(ShutdownThread.java:124)
十月七日至5日:18:21.499:W / System.err的(709):在aexp.keygen.KeyGen $ 1.onClick(KeyGen.java:47)
十月七日至5日:18:21.499:W / System.err的(709):在android.view.View.performClick(View.java:3511)
十月七日至5日:18:21.499:W / System.err的(709):在android.view.View $ PerformClick.run(View.java:14105)
十月七日至5日:18:21.509:W / System.err的(709):在android.os.Handler.handleCallback(Handler.java:605)
十月七日至5日:18:21.509:W / System.err的(709):在android.os.Handler.dispatchMessage(Handler.java:92)
十月七日至5日:18:21.509:W / System.err的(709):在android.os.Looper.loop(Looper.java:137)
十月七日至5日:18:21.509:W / System.err的(709):在android.app.ActivityThread.main(ActivityThread.java:4424)
十月七日至5日:18:21.509:W / System.err的(709):在java.lang.reflect.Method.invokeNative(本机方法)
十月七日至5日:18:21.509:W / System.err的(709):在java.lang.reflect.Method.invoke(Method.java:511)
十月七日至5日:18:21.509:W / System.err的(709):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
十月七日至5日:18:21.509:W / System.err的(709):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
十月七日至5日:18:21.509:W / System.err的(709):在dalvik.system.NativeStart.main(本机方法)
 

其他的方法不抛出任何错误。我需要帮助来完成这一任务,它曾经方式,它需要即使我必须调用本地方法来完成,我会做到这一点,但我必须显示关掉UI。

感谢

Ashwani

解决方案

第六方式:

  $亚行外壳输入的keyEvent 26
 

作品!

第七方式:

如果你想长期preSS

  $ ADB壳LT;<!
>的SendEvent的/ dev /输入/ event5 1 107 1
>睡眠1
>的SendEvent的/ dev /输入/ event5 1 107 0
>出口
> !
 

您的设备可以使用不同的输入设备比的/ dev /输入/ event5

第八办法

这方法是多一点聪明,使用的SendEvent 输入的KeyEvent 根据API级别。

 #!在/ usr /斌/包膜蟒蛇

从com.dtmilano.android.adb.adbclient进口*

AdbClient(系列号='你的,序列号,在这里)。长preSS(权力)
 

这里的优点是,这是一种通用的方法和可用于发给其他键。 AdbClient 是一个Python实现亚洲开发银行和分布是 AndroidViewClient /库莱布拉

I am trying to invoke the switch off dialog box that appears when we press power button. But i want to accomplish this task from an android application or a Junit test case. I will choose the most feasible approach in this case. I have been trying to do this but was not able to succeed. I am trying to use the following five approaches:

First approach:

long eventTime = SystemClock.uptimeMillis(); boolean
                  dispateched = launcherButtonWInst.dispatchKeyEvent(new
                  KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
                  KeyEvent.KEYCODE_POWER, 0, 0, 0, 0, 0) );
                  Log.i(LOG_TAG,String.valueOf(dispateched)); boolean
                  dispateched2=launcherButtonWInst.dispatchKeyEvent(new
                  KeyEvent(eventTime+2000, eventTime+2000, KeyEvent.ACTION_UP,
                  KeyEvent.KEYCODE_POWER, 0, 0, 0, 0, 0) );
                  Log.i(LOG_TAG,String.valueOf(dispateched2));

=======================================================

Second apporach:

private void generateKeys() {
        // Obtain the WindowManager system service interface
        IBinder wmbinder = ServiceManager.getService("window");
        Log.d(LOG_TAG, "WindowManager: " + wmbinder);
        IWindowManager wm = IWindowManager.Stub.asInterface(wmbinder);
        keyUpDown(wm, KeyEvent.KEYCODE_POWER);
    }

    private void keyUpDown(IWindowManager wm, int keycode) {
        try {
            Log.d(LOG_TAG, "keyDown: " + keycode);
            wm.injectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keycode), true);
            Log.d(LOG_TAG, "keyUp: " + keycode);
            wm.injectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keycode), true);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            Log.e("ERROR!!!!!", e.toString());
            e.printStackTrace();
        }
    }

============================================================

Third approach:

Instrumentation inst = new Instrumentation();
        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POWER);

======================================================

Fourth approach:

try {
                         long now =SystemClock.uptimeMillis();
                         KeyEvent down = new KeyEvent(now, now,
                         KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER, 0);

                         Log.d( LOG_TAG,down.toString() );
                         KeyEvent up = new KeyEvent(now+2000, now+2000,
                         KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER, 0);
                         Log.d( LOG_TAG,up.toString() );
                         (IWindowManager.Stub.asInterface(ServiceManager.getService("window"))).injectKeyEvent(down,
                         true);
                         try {
                         Thread.sleep(2000);
                         } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         }
                         (IWindowManager.Stub.asInterface(ServiceManager.getService("window"))).injectKeyEvent(up,
                         true);
                         } catch (RemoteException e) {
                         Log.d("LOGTAG",
                         "SendKeyEvent exception:"+e.getMessage());

}

Fifth approach:

try {
                    Context mContext = getBaseContext();
                    Dialog dialog=new Dialog(mContext);
                    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    ShutdownThread.shutdown(mContext, true);

                } catch (Exception e) {
                    Log.e("ERROR!!!", e.toString());
                    e.printStackTrace();
                }

===================================================================

But none of them is working for me. The fifth approach is invoking shutdown() method of ShutdownThread class. But it gives me following error:

07-05 10:18:21.489: W/System.err(709): android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@4104d798 -- permission denied for this window type
07-05 10:18:21.499: W/System.err(709):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:537)
07-05 10:18:21.499: W/System.err(709):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
07-05 10:18:21.499: W/System.err(709):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
07-05 10:18:21.499: W/System.err(709):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
07-05 10:18:21.499: W/System.err(709):     at android.app.Dialog.show(Dialog.java:278)
07-05 10:18:21.499: W/System.err(709):     at com.android.internal.app.ShutdownThread.shutdown(ShutdownThread.java:124)
07-05 10:18:21.499: W/System.err(709):     at aexp.keygen.KeyGen$1.onClick(KeyGen.java:47)
07-05 10:18:21.499: W/System.err(709):     at android.view.View.performClick(View.java:3511)
07-05 10:18:21.499: W/System.err(709):     at android.view.View$PerformClick.run(View.java:14105)
07-05 10:18:21.509: W/System.err(709):     at android.os.Handler.handleCallback(Handler.java:605)
07-05 10:18:21.509: W/System.err(709):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 10:18:21.509: W/System.err(709):     at android.os.Looper.loop(Looper.java:137)
07-05 10:18:21.509: W/System.err(709):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-05 10:18:21.509: W/System.err(709):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 10:18:21.509: W/System.err(709):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 10:18:21.509: W/System.err(709):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-05 10:18:21.509: W/System.err(709):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-05 10:18:21.509: W/System.err(709):     at dalvik.system.NativeStart.main(Native Method)

Other approaches do not throw any error. I need help to accomplish this task, which ever way it takes even though I have to invoke native methods to accomplish I will do that, but I have to display the switch off UI.

Thanks

Ashwani

解决方案

Sixth approach:

$ adb shell input keyevent 26

works!

Seventh approach:

If you want the long press

$ adb shell <<!
> sendevent /dev/input/event5 1 107 1
> sleep 1
> sendevent /dev/input/event5 1 107 0
> exit
> !

You device may use a different input device than /dev/input/event5

Eighth approach

This approach is a bit more clever and uses sendevent or input keyevent depending on API level.

#! /usr/bin/env python

from com.dtmilano.android.adb.adbclient import *

AdbClient(serialno='your-serial-number-here').longPress('POWER')

The advantage here is that this is a generic approach and can be used to send other keys too. AdbClient is a python implementation of adb and is distributed with AndroidViewClient/culebra.

这篇关于模拟电源按钮preSS显示关机对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆