[Interest] [ Android ] How pass argument to QtActivity::runOnUiThread ?

Eddie Sutton eddie.of.the.spam at gmail.com
Wed Sep 28 20:01:56 CEST 2016


Hi Jason,


Thanks for wakeLock recommendation.  

I may end up using wakeLock.  Especially if it gives me a wider range of supported Android devices and versions.


I ended up using a “final boolean” :

http://stackoverflow.com/questions/7761723/android-howto-pass-data-to-the-runnable-in-runonuithread <http://stackoverflow.com/questions/7761723/android-howto-pass-data-to-the-runnable-in-runonuithread>

    /// Keep device from going to sleep during long
    /// periods of no touch-interaction such as a Bluetooth file transfer.
    ///
    ///
    /// \sa https://developer.android.com/training/scheduling/wakelock.html
    ///
    /// Unlike wake locks, FLAG_KEEP_SCREEN_ON doesn't require special permission, and
    /// the platform correctly manages the user moving between applications, without
    /// your app needing to worry about releasing unused resources.
    ///
    /// \param[in] keepScreenOn set true to prevent power saving
    public static void setKeepScreenOn(final boolean keepScreenOn) {

        m_activity.runOnUiThread(new Runnable() {
            public void run() {
                try {
                    Log.d(TAG, String.format("setKeepScreenOn: %b", keepScreenOn));

                    if (keepScreenOn) {
                        m_activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                        return;
                    }

                    // Else let it go back to sleep and save power
                     m_activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

                } catch (Throwable t) {
                    Log.d(TAG, String.format("setKeepScreenOn.exception %s", t.toString()));
                }
            }
        });

    }



-Ed



> On Sep 28, 2016, at 12:38 PM, Jason H <jhihn at gmx.com> wrote:
> 
> Why not have two runnables? One that sets the flag and one that does not?
>  
> m_activity.runOnUiThread( ikeepScreenOn ? new Runnable() { ... keep screen on code... } : new Runnable { ...don't keep screen on... }
> It might boove you to put the common code in one function though.
>  
>  
> I personally use:
> PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
> screenLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE , "RecordingLock");
> Which I then call as needed. However Adnroid has many revisions and this practice may be obsolete.
>  
> Sent: Wednesday, September 28, 2016 at 1:23 PM
> From: "Eddie Sutton" <eddie.of.the.spam at gmail.com>
> To: "Qt Interest" <interest at qt-project.org>
> Subject: [Interest] [ Android ] How pass argument to QtActivity::runOnUiThread ?
> How can you pass an argument to QtActivity runOnUiThread ?
>  
>  
> Example with no argument:
>  
>     public void hide()
>     {
>         m_activity.runOnUiThread( new Runnable() {
>             @Override
>             public void run() {
>                 if (m_dialog != null && m_dialog.isShowing())
>                     m_dialog.dismiss();
>                 reset();
>             }
>         });
>     }
>  
>  
>  
> My goal is to temporarily disable power saving during a log-running Bluetooth transfer by clearing the Window flag FLAG_KEEP_SCREEN_ON
>  
>     public void setKeepScreenOn(boolean keepScreenOn)
>  
>     {
>         m_activity.runOnUiThread( new Runnable() {
>             @Override
>             public void run() {
>  
> if (keepScreenOn) {
>                 m_instance.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
>                 return;
>             }
>  
>             // Else let it go back to sleep and save power
>             m_instance.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
>  
>             }
>         });
>     }
>  
>  
>  
> -Ed
> _______________________________________________ Interest mailing list Interest at qt-project.org http://lists.qt-project.org/mailman/listinfo/interest <http://lists.qt-project.org/mailman/listinfo/interest>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160928/6cb4a2e8/attachment.html>


More information about the Interest mailing list