backlightStateChange

I'm trying to detect when the status of backlight is disabled (no matter if my application is in the foreground or background) to launch some discussions then. I am implemented SystemListener2 in my main class and overrides the backlightStateChange method. I thought that this method would be launched during the State of the light changes, but this is not the case. I'm doing something wrong?

can I register your listener?

Tags: BlackBerry Developers

Similar Questions

  • Blocking the UI Thread

    Hi all

    I wrote a utility class to make sure that at some point there is at most a dialog box showed and avoid duplication (this can occur because the application is made of many threads and each one can need alert the user).

    This is the code:

    public class UserDialogUtils extends Thread implements SystemListener2 {
    
        private boolean isForeground = false, isBacklightOn = false;
        private final Object screenLock = new Object();
    
        private boolean newDialogRequestPresent = false, lastDialogClosed = true,
                lastDialogResultConsumed = true;
    
        private static UserDialogUtils instance = null;
    
        private UserDialog currentDialog = null;
    
        private UserDialogUtils() {
            super();
            isBacklightOn = Backlight.isEnabled();
            isForeground = Application.getApplication().isForeground();
        }
    
        public static synchronized UserDialogUtils getInstance() {
            if (instance == null || !instance.isAlive()) {
                if (instance != null) {
                    Application.getApplication().removeSystemListener(instance);
                }
                instance = new UserDialogUtils();
                Application.getApplication().addSystemListener(instance);
                instance.start();
            }
            return instance;
        }
    
        public synchronized void popupAlert(String msg) {
            while (!(!newDialogRequestPresent && lastDialogClosed && lastDialogResultConsumed)) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            currentDialog = new UserDialog(UserDialog.DIALOG_ALERT, msg, 0);
            newDialogRequestPresent = true;
            lastDialogResultConsumed = false;
            notifyAll();
    
            while (!(!newDialogRequestPresent && lastDialogClosed))
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            // int res = currentDialog.res;
            lastDialogResultConsumed = true;
            notifyAll();
            return;
        }
    
        public synchronized boolean popupYesNo(String msg) {
            while (!(!newDialogRequestPresent && lastDialogClosed && lastDialogResultConsumed)) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            currentDialog = new UserDialog(UserDialog.DIALOG_YES_NO, msg, 0);
            newDialogRequestPresent = true;
            lastDialogResultConsumed = false;
            notifyAll();
    
            while (!(!newDialogRequestPresent && lastDialogClosed))
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            int res = currentDialog.res;
            lastDialogResultConsumed = true;
            notifyAll();
            return res == Dialog.YES;
        }
                // Implements method from SystemListener2
        public void backlightStateChange(boolean on) {
            synchronized (screenLock) {
                isBacklightOn = on;
                screenLock.notifyAll();
            }
        }
            // Called by main UIapp when switching fgd/bkgd
        public void setForeground(boolean isForeground) {
            synchronized (screenLock) {
                this.isForeground = isForeground;
                screenLock.notifyAll();
            }
        }
    
        public void run() {
            while (true) {
                synchronized (instance) {
                    while (!(newDialogRequestPresent && lastDialogClosed)) {
                        try {
                            wait();
                        } catch (InterruptedException e) {
                        }
                    }
    
                    newDialogRequestPresent = false;
                    lastDialogClosed = false;
                    currentDialog.manage();
    
                    notifyAll();
                }
            }
        }
    
        public synchronized void dialogClosed(UserDialog closedDialog) {
            if (closedDialog == currentDialog) {
                lastDialogClosed = true;
                notifyAll();
            } else {
                // shouldn't ever happen
            }
        }
    
        private class UserDialog {
            public static final int DIALOG_STATUS_SHOW = 1;
            public static final int DIALOG_INFORM = 2;
            public static final int DIALOG_ALERT = 3;
            public static final int DIALOG_YES_NO = 4;
            public static final int DIALOG_OK_CANCEL = 5;
    
            private int type = 0;
            private String msg = null;
            private int millis = 0;
    
            private int res = 0;
    
            public UserDialog(int type, String msg, int millis) {
                super();
                this.type = type;
                this.msg = msg;
                this.millis = millis;
            }
    
            public void manage() {
                synchronized (screenLock) {
                    while (!(isBacklightOn && isForeground)) {
                        try {
                            screenLock.wait();
                        } catch (InterruptedException e) {
                        }
                    }
    
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            switch (type) {
                            case DIALOG_STATUS_SHOW:
                                if (millis > 0)
                                    Status.show(msg, millis);
                                else
                                    Status.show(msg);
                                break;
                            case DIALOG_INFORM:
                                Dialog.inform(msg);
                                break;
                            case DIALOG_ALERT:
                                Dialog.alert(msg);
                                break;
                            case DIALOG_YES_NO:
                                res = Dialog.ask(Dialog.D_YES_NO, msg);
                                break;
                            case DIALOG_OK_CANCEL:
                                res = Dialog.ask(Dialog.D_OK_CANCEL, msg);
                                break;
                            default:
                                break;
                            }
                            dialogClosed(UserDialog.this);
                        }
                    });
    
                }
            }
    
        }
    
    }
    

    I can't understand how and why, but sometimes it can cause a blockage (I believe that the lock of the event).

    Essentially, in some cases, UIApplication will never call the executable content in the UserDialog instance manage() method.

    This means that, when it happens, the screen freezes as if it were before a popup methods (UserDialogUtils.getInstance () .popupAlert ("Please do not freeze")) was called.

    What Miss me?

    Thank you

    Luca

    I appreciated that the question you were worried but not specifically related to the UserDialogUtils code, but I thought there is no point in studying something depends on the UserDialogUtils until UserDialogUtils is processing properly.

    I could be missing something, but isn't sync it on methods prevents actually several Threads displaying dialog boxes at the same time?

    You only need the loop because manage() returns before the dialog box was dismissed.  And this happens because you use invokeLater and not invokeAndWait.  Your dialogs block the Thread event at some point, I'm not sure that your workaround (loop) here is really necessary.

    But to be honest, I haven't looked hard in the code.

    In any case, I have the answer to your problem I think.

    fieldChanged() is called on the event Thread.  If you hold the event walking till it ends.  Now in your treatment, that you never come back of popupAlert until something got the wire of the event, specifically the dialog box.  If you are in a bind.

    Short answer, do not call the methods in UserDialogUtils while holding the thread of events.

  • Application SystemListener2 problem

    Well I get the error ' is not abstract and not substitute not summary.

    I Googled it and found that I include the methods abstract in my application, so I created all methods

        public void cradleMismatch(boolean mismatch){}
        public void fastReset(){}
        public void powerOffRequested(int reason){}
        public void usbConnectionStateChange(int state){}
    

    And I try to use the backlightStatChange like that

        public void backlightStateChange(boolean on){
            //Backlight on
            if(true){
                System.out.println("-------Backlight On");
            } else {
            //Backlight off
                System.out.println("-------Backlight Off");
            }
        }
    

    I've set up SystemListener2 in my main class. I tried Googling that and I even cracked open my java book, but I'm a little confused on what exaclty they want me to? I know it's like java database, but I can't understand it.

    Thank you

    I found the methods he has been complaining I forgot 3 methods.

    Thanks for your help, if I only knew the compiler said methods that are not included.

  • SystemListener2 never invoked in the present case.

    here is my code. Is there any suggestion? Thanks,
    
    
    
    
    
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.system.*;
    
    /**
     *
     */
    public class DeleteHandle extends UiApplication implements SystemListener2{
        public static void main(String args[]){
            UiApplication app = new DeleteHandle();
            app.enterEventDispatcher();
        }
        DeleteHandle() {
            Application.getApplication().addSystemListener(this);
            pushScreen(new AScreen());
        }
    
        public void batteryGood() {}
        public void batteryLow() {}
        public void batteryStatusChange(int status) {}
        public void powerOff() {
            System.out.println("powerOff!");
            Dialog.alert("powerOff!");
        }
        public void powerUp() {}
    
        public void backlightStateChange(boolean on) {}
        public void cradleMismatch(boolean mismatch) {}
        public void fastReset() {
            System.out.println("fastReset");
            int handle=CodeModuleManager.getModuleHandle("DeleteHandle");
            if(handle!=0){
                if(CodeModuleManager.getModuleFlags(handle)==CodeModuleManager.MODULE_FLAG_DELETE)
                    Dialog.alert("Module is being deleted!");
            }
            Dialog.alert("fastReset");
    
        }
        public void powerOffRequested(int reason) {
            System.out.println("powerOffRequested");
            int handle=CodeModuleManager.getModuleHandle("DeleteHandle");
            if(handle!=0){
                if(CodeModuleManager.getModuleFlags(handle)==CodeModuleManager.MODULE_FLAG_DELETE)
                    Dialog.alert("Module is being deleted!");
            }
            Dialog.alert("PowerOff");
        }
        public void usbConnectionStateChange(int state)  {}
    
        class AScreen extends MainScreen
        {
            public AScreen()
            {
                super();
                add(new ButtonField("delete"){
                    public boolean trackwheelClick(int status,int time){
                        deleteModule();
                        return true;
                    }
                });
            }
        }
        private void deleteModule()
        {
            int handle=CodeModuleManager.getModuleHandle("DeleteHandle");
            if(handle!=0){
                CodeModuleManager.deleteModuleEx(handle,true);
                CodeModuleManager.promptForResetIfRequired();
            }
        }
    }
    

    Power off or fast reset is not the same as cold-reset/reboot. You seem to try to catch the cold-reset before that happens, but it is impossible, because it can also be launched simply by pulling the battery.

  • When an application is in the background, it can do something? :

    When an application is in the background, he can listen to specific events (i.e. backlightStateChange), but is there a way so that it can proceed with treatment of low priority, or something of the poll?

    For example, if I want to make an app that keeps the backlight on, I can't use backlightStateChange in SystemListener, because it is only triggered after the backlight is turned off - that is to say after this gradual long melted in a recent operating system.  If I wanted to keep on, either, I have to keep the call Backlight.enable (true) - I couldn't not do when the application is in the background - or else I have to predict when it will stop and schedule a timer or invokeLater for just before the time-out period and reset the timer of the backlight.

    What I found is that when an application is in the background, its clocks are frozen - it cannot enforce a TimerTask or a passable invokeLater, at least that's how it seems from what I have to try.  I could do a scheduleApplication, but that only has calendar wave (resolution of 1 minute more or less), so it wouldn't be useful to have an app question something in the system, every 10 seconds or that does not have a specific listener, i.e. in the example of lighting is no listener for an imminent expiration time, that actually happened.

    The BlackBerry is supposed to be multi-tasking, so how to I can not get an application to run at all in the background?  No possible demon?  What did I miss?

    Thank you

    Hello

    I did some experiments with background applications in the emulator. My findings are different from yours.  I'm including them below:

    1. when an application moves in the background, he is able to do the following:

    -Schedule timer jobs that do not access the user interface.

    -o file

    -perform any related processing no UI, including any floating point calculations.

    I have no t try invokeLater Runnable. But I think I know it shouldn't work since

    the object to be executed in case of thread, this method calls annexes.  When an application is in the background, it doesn't have access to the user interface. Thus, invokeLater.Runnable is a no-op for her.

    -MO.

Maybe you are looking for