1

I am programming simple game. Draw/Update go throw realtime model, so CPU is always high loaded. Thread looks so:


    @Override
    public void run() {
        while (true) {   
            if (!mRun) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {};
            } else {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        ..update game objects..
                        if (canvas != null) {
                        ..draw to canvas...
                        }
                    }
                } finally {
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }                
        }
    }

Problem is that working so application eats battery. On my Galaxy S2 it drains faster than charges in the same moment being on PC USB.

What am I doing wrong, or it's OK for games?

Vadim
  • 1,582
  • 2
  • 15
  • 16
  • Do you call onDrawcanvas) or render(canvas)? onDraw() is supposed to leverage hardware screen buffers so its not a purely CPU hard loop. Take a look at: http://stackoverflow.com/questions/6970987/android-surfaceview-displays-blank-when-locked-canvas-is-not-null – dubmojo Jul 28 '13 at 16:21
  • As for USB charging, the S2 is USB 2.0 and you're charging off a laptop/desktop. There is a dedicated 5v line in a USB cable, but the spec was released in 2000 for devices at the time. Your S2 will try draw more than it can for the CPU, WIFI, Bluetooth, screen, etc... are all on. Plus your S2 is old and the battery is likely can't fully charge anymore. Android devices generally have issues with charging. My Google Nexus 10 can't charge faster than WIFI web browsing will drain. All I can suggest is you compare your drain rate to a 3DS or PSP Vita. (3 hours with WIFI on) – dubmojo Jul 28 '13 at 16:31

2 Answers2

3

Charging from a USB port in your PC is going to be very slow (the USB is also transferring data, not just power). I would think it is okay for a game to use more charge than it is receiving when connected to a PC USB port.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • I would say Angry Birds / Bad Piggies do not disharge so much, aren't they? – Vadim Jun 24 '13 at 08:23
  • USB will transfer data only when needed and for most cases, when charging over USB, there is no data transfer (unless one is using adb to do some operations in parallel). But the charge current from USB is typically lower than wall-mounted charging. – Kumar Rangarajan Jun 25 '13 at 02:40
1

Discharge current from the phone would depend on what its doing (esp what the apps are doing). Typically screen consumes a big part of the power, and the color of the app (white vs black is a big difference) and the brightness levels make a big impact. And if you are consuming CPU (and maybe data) at the same time, they would impact for sure.

In our tests of profiling Angry Birds for power, we have seen it is quite well behaved and does not consume too much power (including CPU consumption). And also they typically use slightly darker colors too. And their data consumption is typically only when there are ads (which is a big consumer, and we noticed they have increased in recent times for sure).

I would check to see what components of your app is draining battery and see if it can be optimized. If you are interested, try out profiling tools like Little Eye (disclaimer: I am the co-founder of the company which builds this tool) to see which part (or h/w component usage) of your application is draining battery.

  • Thank you. But is it OK for game to load CPU with endless loop? – Vadim Jun 25 '13 at 09:50
  • That would be quite bad actually. Because the system (even when say the display is turned off) might never goto sleep. So ideally, running an continuously loop (which does not block or rather waits) would not be an optimal thing to do. – Kumar Rangarajan Jun 28 '13 at 05:04
  • Being turned off it will go to sleep, because thread will be stopped when screen is off. Actually, I am not sure about waits. How often and how long shoudl it wait? Perhaps I have to look to actual FPS and don't allow it to be too much (using waits) – Vadim Jun 28 '13 at 08:59
  • Well not all threads will be stopped. Only the UI thread will stop or rather no events will be supplied to the thread and so will block/sleep. But if you have a worker thread that is doing some work in the background, it will continue to run. Again you can use a tool like Little Eye (sorry to keep selling, but felt it would be relevant here) to validate this theory. You can see if your CPU activity dies after you turn off the screen. – Kumar Rangarajan Jun 28 '13 at 09:24