在应用程序运行时节省能源



我是安卓编程的新手。我写了一个应用程序,可以读取传感器数据(陀螺仪和加速度计),并且在传递值时应该做一些事情。现在我的问题是电池只运行一个小时,sensor_delay_fastestsensor_delay_game之间没有区别。是否有可能转动屏幕灯以节省能源,或者我应该将此应用程序作为服务运行,或者是否有另一种可能性可以执行此操作?我也尝试了SCREEN_DIM_WAKE_LOCK,没有显着效果。

感谢您的帮助

贡纳尔

您可能将电池消耗归咎于错误的罪魁祸首。电池消耗的最大坏蛋是没有在主应用程序循环中使用 Thread.sleep(int ms)。

您的主循环可能每秒运行数千次,而实际上实现帧限制可以将数量减少到 24-60(用于流畅动画),甚至对于非常基本的应用程序,甚至低至 10-20。

考虑一下:如果你不休眠你的线程,你实际上是让你的应用程序(和主循环)尽可能快地运行。如果没有睡眠,你的应用将消耗 100% 的可用资源来运行,即使它几乎什么都不做。

这是一个很好的入门教程,下面是一个实现示例(来自链接)。不要担心它有多长;没有评论就短了很多...

public void run() {
    Canvas canvas;
    Log.d(TAG, "Starting game loop");
    // initialise timing elements for stat gathering
    initTimingElements();
    long beginTime;     // the time when the cycle begun
    long timeDiff;      // the time it took for the cycle to execute
    int sleepTime;      // ms to sleep (<0 if we're behind)
    int framesSkipped;  // number of frames being skipped 
    sleepTime = 0;
    while (running) {
        canvas = null;
        // try locking the canvas for exclusive pixel editing
        // in the surface
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                beginTime = System.currentTimeMillis();
                framesSkipped = 0;  // resetting the frames skipped
                // update game state 
                this.gamePanel.update();
                // render state to the screen
                // draws the canvas on the panel
                this.gamePanel.render(canvas);              
                // calculate how long did the cycle take
                timeDiff = System.currentTimeMillis() - beginTime;
                // calculate sleep time
                sleepTime = (int)(FRAME_PERIOD - timeDiff);
                if (sleepTime > 0) {
                    // if sleepTime > 0 we're OK
                    try {
                        // send the thread to sleep for a short period
                        // very useful for battery saving
                        Thread.sleep(sleepTime);    
                    } catch (InterruptedException e) {}
                }
                while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                    // we need to catch up
                    this.gamePanel.update(); // update without rendering
                    sleepTime += FRAME_PERIOD;  // add frame period to check if in next frame
                    framesSkipped++;
                }
                if (framesSkipped > 0) {
                    Log.d(TAG, "Skipped:" + framesSkipped);
                }
                // for statistics
                framesSkippedPerStatCycle += framesSkipped;
                // calling the routine to store the gathered statistics
                storeStats();
            }
        } finally {
            // in case of an exception the surface is not left in 
            // an inconsistent state
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }   // end finally
    }
}

相关内容

  • 没有找到相关文章

最新更新