Qt on Android : How to shutdown the device or simulate the press of Power button



  • I'd like to build a small application that reboot the device or better put in PowerOff my device.

    I saw that there are in qt the specific class and the I have call a JNI java class.

    I have assign the right permissiom to my app following:

    @<uses-permission android:name="android.permission.REBOOT"/>
    <uses-permission android:name="android.permission.DEVICE_POWER"/>
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>@

    and the call in qt c++ to my JNI class is the following:

    @QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/PowerOff/Next72Utility", "powerOff", "()V");@

    and external java class is the following:

    @//
    // Next72Utility.java
    //
    package org.qtproject.example.PowerOff;

    import android.content.Context;

    import android.app.Activity;
    import android.view.WindowManager;
    import android.view.WindowManager.LayoutParams;
    import android.os.PowerManager;

    public class Next72Utility extends org.qtproject.qt5.android.bindings.QtActivity
    {

    public static Next72Utility m_istance;

    public Next72Utility()
    {
    m_istance = this;
    }

    public static void powerOff()
    {
    if (m_istance != null)
    {
    System.out.println("I'm here");
    PowerManager pm = (PowerManager) m_istance.getSystemService(Context.POWER_SERVICE);
    System.out.println("pm = " + pm);
    pm.reboot(null);

       }
    

    }

    @}

    The program is compiled without error and the output (console) is the following (no errors):

    @Starting remote process.D/dalvikvm(24655): Late-enabling CheckJNI
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/lib/libgnustl_shared.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/lib/libgnustl_shared.so 0x4186f148
    D/dalvikvm(24655): No JNI_OnLoad found in /data/data/org.qtproject.example.PowerOff/lib/libgnustl_shared.so 0x4186f148, skipping init
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/lib/libQt5Core.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/lib/libQt5Core.so 0x4186f148
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/lib/libQt5Gui.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/lib/libQt5Gui.so 0x4186f148
    D/dalvikvm(24655): No JNI_OnLoad found in /data/data/org.qtproject.example.PowerOff/lib/libQt5Gui.so 0x4186f148, skipping init
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/lib/libQt5Widgets.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/lib/libQt5Widgets.so 0x4186f148
    D/dalvikvm(24655): No JNI_OnLoad found in /data/data/org.qtproject.example.PowerOff/lib/libQt5Widgets.so 0x4186f148, skipping init
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/lib/libQt5AndroidExtras.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/lib/libQt5AndroidExtras.so 0x4186f148
    D/dalvikvm(24655): No JNI_OnLoad found in /data/data/org.qtproject.example.PowerOff/lib/libQt5AndroidExtras.so 0x4186f148, skipping init
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/qt-reserved-files/plugins/platforms/android/libqtforandroid.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/qt-reserved-files/plugins/platforms/android/libqtforandroid.so 0x4186f148
    I/Qt (24655): qt start
    D/dalvikvm(24655): Trying to load lib /data/data/org.qtproject.example.PowerOff/lib/libPowerOff.so 0x4186f148
    D/dalvikvm(24655): Added shared lib /data/data/org.qtproject.example.PowerOff/lib/libPowerOff.so 0x4186f148
    D/dalvikvm(24655): No JNI_OnLoad found in /data/data/org.qtproject.example.PowerOff/lib/libPowerOff.so 0x4186f148, skipping init
    D/dalvikvm(24655): GC_CONCURRENT freed 341K, 12% free 7160K/8135K, paused 2ms+2ms, total 27ms
    D/dalvikvm(24655): WAIT_FOR_CONCURRENT_GC blocked 10ms
    D/dalvikvm(24655): WAIT_FOR_CONCURRENT_GC blocked 10ms
    D/libEGL (24655): loaded /system/lib/egl/libEGL_mali.so
    D/libEGL (24655): loaded /system/lib/egl/libGLESv1_CM_mali.so
    D/libEGL (24655): loaded /system/lib/egl/libGLESv2_mali.so
    D/ (24655): Device driver API match
    D/ (24655): Device driver API version: 10
    D/ (24655): User space API version: 10
    D/ (24655): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
    D/OpenGLRenderer(24655): Enabling debug mode 0
    I/System.out(24655): I'm here
    I/System.out(24655): pm = android.os.PowerManager@418c2040
    "org.qtproject.example.PowerOff" terminated.@

    but the device is until running......
    Do you have a solution for this problem ?



  • I have read several article and I understand that

    1. to shutdown the device you have to ROOT permission

    2. you can put in sleep mode without ROOT permission but not in shutdown.

    Observation

    I have a SAMSUNG NOTE 10.1 (tablet) and when I press the power button for 1 second the device go to sleep.

    If I press the power button for 3 seconds I have a dialog screen that it propose if I turn off the device or not.

    Then I want to simulate the second case (button for 3 second the power button).

    @void keyPressPowerButton()
    {
    QKeyEvent event(QEvent::KeyPress, Qt::Key_PowerOff, Qt::NoModifier);
    QApplication::sendEvent(mainWindow, &event);
    }@

    But It go in sleep mode not it in shutdown mode (with dialog screen that it propose if I turn off the device or not)

    How can I simulate the Qt::Key_PowerOff for 3 seconds ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.