Android - onPause 和 WidgetLocker

发布于 2024-12-27 11:46:42 字数 9381 浏览 0 评论 0原文

我正在编写一个基本应用程序来测试加速度计的行为。目前,我正在注册和注销加速度计,如下所示:

public void pause() {
    mSensorManager.unregisterListener(this);
}

public void resume(Context context) {
    mSensorManager.registerListener(this, mAccelerometer,     
            SensorManager.SENSOR_DELAY_FASTEST);        
}

我有由 Activity 触发的暂停和恢复方法,因为加速度计在子类中运行。当我锁定屏幕时,程序中断。实际上我必须结束该过程才能使程序再次运行。我目前正在使用 WidgetLocker。该程序与库存锁屏配合良好。我的假设是简历永远不会被运行。

有没有办法确保传感器被重新注册?如果他们使用正常锁屏之外的任何内容,我不希望我的程序中断。

编辑:添加了完整源代码。 的底部

有关此问题的主要逻辑发生在 MainGamePanel.java TiltBallActivity.java

package com.tornquist.nathan;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class TiltBallActivity extends Activity{
    /** Called when the activity is first created. */
    MainGamePanel viewPanel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Window state functions.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //This works without declaring a viewPanel instance here.
        //The instance declaration is needed to pass the 
        //onPause and onResume commands though.
        viewPanel = new MainGamePanel(this);
        setContentView(viewPanel);
    }

    //Restarts the accelerometer after onPause
    protected void onResume() {

        viewPanel.resume(this);
        super.onResume();
    }

    //Standard Method run when the Application loses focus.
    //This runs the pause() function in the viewPanel so that
    //the accelerometer can be paused.
    protected void onPause() {

        viewPanel.pause();
        super.onPause(); 
    }
}

MainThread.java

package com.tornquist.nathan;

import com.tornquist.nathan.MainGamePanel;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

    private SurfaceHolder surfaceHolder;
    private MainGamePanel gamePanel;
    private boolean running;
    public void setRunning(boolean running) {
        this.running = running;
    }

    public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }

    @Override
    public void run() 
    {
        Canvas canvas;
        while (running) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    // update game state
                    this.gamePanel.update();

                    // draws the canvas on the panel
                    this.gamePanel.onDraw(canvas);
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }
}

MainGamePanel.java

package com.tornquist.nathan;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements SensorEventListener, SurfaceHolder.Callback 
{
    //Variable Declarations.
    private MainThread thread;
    public int xColor;
    public int yColor;
    public int zColor;

    public float xPos;
    public float yPos;
    public float oldXPos;
    public float oldYPos;

    public int screenWidth;
    public int screenHeight;

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;    

    Paint paint;

    public MainGamePanel(Context context)
    {
        //Standard Initialization
        super(context);

        //This line adds a callback for the touch screen.  
        //This allows you to actually capture touch input.
        getHolder().addCallback(this);

        thread = new MainThread(getHolder(),this);

        xColor = 100;
        yColor = 100;
        zColor = 100;

        paint = new Paint();
        paint.setAntiAlias(true);

        Display display = ((Activity) context).getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();

        yPos = screenHeight/2;
        xPos = screenWidth/2;
        oldYPos = yPos;
        oldXPos = xPos;

        //This registers the accelerometer.  Without registering it, the onSensorChanged
        //event will never be called, and you cannot get the accelerometer values.
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);

        //Also required for touch input.
        setFocusable(true);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // at this point the surface is created and
        // we can safely start the game loop
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        // check if in the lower part of the screen we exit
            if (event.getY() > getHeight() - 50) {
                thread.setRunning(false);
                ((Activity)getContext()).finish();
            }

            if (xColor < 235)
                xColor = xColor + 20;
            else
                xColor = 0;
            if (yColor < 235)
                yColor = yColor + 20;
            else
                yColor = 0;
            if (zColor < 235)
                zColor = zColor + 20;
            else
                zColor = 0;

            yPos = event.getY();
            xPos = event.getX();
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
        //canvas.drawColor(Color.CYAN);
        canvas.drawColor(Color.rgb(xColor, yColor, zColor));
        int xInvert = (int) (255 - xColor);
        int yInvert = (int) (255 - yColor);
        int zInvert = (int) (255 - zColor);

        paint.setColor(Color.rgb(xInvert, yInvert, zInvert));
        paint.setStyle(Style.FILL);
        canvas.drawCircle(xPos, yPos, 50, paint);
    }

    public void update() {
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {        
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        oldYPos = yPos;
        oldXPos = xPos;

        xColor = (int) (255 + (event.values[0])*11);
        if (xColor > 255)
            xColor = 255;
        if (xColor < 0)
            xColor = 0;

        yColor = (int) (255 + (event.values[1])*11);
        if (yColor > 255)
            yColor = 255;
        if (yColor < 0)
            yColor = 0;

        zColor = (int) (255 + (event.values[2])*11);
        if (zColor > 255)
            zColor = 255;
        if (zColor < 0)
            zColor = 0;

        xPos = xPos + -1*(event.values[0])*5;
        yPos = yPos + event.values[1]*5;

        if (xPos < 50)
            xPos = 50;
        if (xPos > screenWidth - 50)
            xPos = screenWidth - 50;
        if (yPos < 50)
            yPos = 50;
        if (yPos > screenHeight - 50)
            yPos = screenHeight - 50;

        if ((oldYPos == yPos) && (oldXPos == xPos))
        {
            invalidate();
        }

    }

    public void pause() {
        mSensorManager.unregisterListener(this);

        //thread.setRunning(false);
        //((Activity)getContext()).finish();
    }

    public void resume(Context context) {
        //mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        //mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);      
    }


}

I am writing a basic application to test the behavior of the accelerometer. Currently I am registering and unregistering the accelerometer like this:

public void pause() {
    mSensorManager.unregisterListener(this);
}

public void resume(Context context) {
    mSensorManager.registerListener(this, mAccelerometer,     
            SensorManager.SENSOR_DELAY_FASTEST);        
}

I have the pause and resume methods triggered by the Activity, as the accelerometer is running in a child class. When I lock the screen the screen, the program breaks. I actually have to end the process to get the program to work again. I am currently using WidgetLocker. The program works fine with the stock lockscreen. My assumption is that resume is never being run.

Is there a way to make sure that the sensor will be reregisterd? I don't want my program to break if they are using anything outside of the normal lockscreen.

EDIT: Full Source Added. The main logic regarding this issue takes place at the bottom of MainGamePanel.java

TiltBallActivity.java

package com.tornquist.nathan;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class TiltBallActivity extends Activity{
    /** Called when the activity is first created. */
    MainGamePanel viewPanel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Window state functions.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //This works without declaring a viewPanel instance here.
        //The instance declaration is needed to pass the 
        //onPause and onResume commands though.
        viewPanel = new MainGamePanel(this);
        setContentView(viewPanel);
    }

    //Restarts the accelerometer after onPause
    protected void onResume() {

        viewPanel.resume(this);
        super.onResume();
    }

    //Standard Method run when the Application loses focus.
    //This runs the pause() function in the viewPanel so that
    //the accelerometer can be paused.
    protected void onPause() {

        viewPanel.pause();
        super.onPause(); 
    }
}

MainThread.java

package com.tornquist.nathan;

import com.tornquist.nathan.MainGamePanel;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

    private SurfaceHolder surfaceHolder;
    private MainGamePanel gamePanel;
    private boolean running;
    public void setRunning(boolean running) {
        this.running = running;
    }

    public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }

    @Override
    public void run() 
    {
        Canvas canvas;
        while (running) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    // update game state
                    this.gamePanel.update();

                    // draws the canvas on the panel
                    this.gamePanel.onDraw(canvas);
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }
}

MainGamePanel.java

package com.tornquist.nathan;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements SensorEventListener, SurfaceHolder.Callback 
{
    //Variable Declarations.
    private MainThread thread;
    public int xColor;
    public int yColor;
    public int zColor;

    public float xPos;
    public float yPos;
    public float oldXPos;
    public float oldYPos;

    public int screenWidth;
    public int screenHeight;

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;    

    Paint paint;

    public MainGamePanel(Context context)
    {
        //Standard Initialization
        super(context);

        //This line adds a callback for the touch screen.  
        //This allows you to actually capture touch input.
        getHolder().addCallback(this);

        thread = new MainThread(getHolder(),this);

        xColor = 100;
        yColor = 100;
        zColor = 100;

        paint = new Paint();
        paint.setAntiAlias(true);

        Display display = ((Activity) context).getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();

        yPos = screenHeight/2;
        xPos = screenWidth/2;
        oldYPos = yPos;
        oldXPos = xPos;

        //This registers the accelerometer.  Without registering it, the onSensorChanged
        //event will never be called, and you cannot get the accelerometer values.
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);

        //Also required for touch input.
        setFocusable(true);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // at this point the surface is created and
        // we can safely start the game loop
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        // check if in the lower part of the screen we exit
            if (event.getY() > getHeight() - 50) {
                thread.setRunning(false);
                ((Activity)getContext()).finish();
            }

            if (xColor < 235)
                xColor = xColor + 20;
            else
                xColor = 0;
            if (yColor < 235)
                yColor = yColor + 20;
            else
                yColor = 0;
            if (zColor < 235)
                zColor = zColor + 20;
            else
                zColor = 0;

            yPos = event.getY();
            xPos = event.getX();
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
        //canvas.drawColor(Color.CYAN);
        canvas.drawColor(Color.rgb(xColor, yColor, zColor));
        int xInvert = (int) (255 - xColor);
        int yInvert = (int) (255 - yColor);
        int zInvert = (int) (255 - zColor);

        paint.setColor(Color.rgb(xInvert, yInvert, zInvert));
        paint.setStyle(Style.FILL);
        canvas.drawCircle(xPos, yPos, 50, paint);
    }

    public void update() {
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {        
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        oldYPos = yPos;
        oldXPos = xPos;

        xColor = (int) (255 + (event.values[0])*11);
        if (xColor > 255)
            xColor = 255;
        if (xColor < 0)
            xColor = 0;

        yColor = (int) (255 + (event.values[1])*11);
        if (yColor > 255)
            yColor = 255;
        if (yColor < 0)
            yColor = 0;

        zColor = (int) (255 + (event.values[2])*11);
        if (zColor > 255)
            zColor = 255;
        if (zColor < 0)
            zColor = 0;

        xPos = xPos + -1*(event.values[0])*5;
        yPos = yPos + event.values[1]*5;

        if (xPos < 50)
            xPos = 50;
        if (xPos > screenWidth - 50)
            xPos = screenWidth - 50;
        if (yPos < 50)
            yPos = 50;
        if (yPos > screenHeight - 50)
            yPos = screenHeight - 50;

        if ((oldYPos == yPos) && (oldXPos == xPos))
        {
            invalidate();
        }

    }

    public void pause() {
        mSensorManager.unregisterListener(this);

        //thread.setRunning(false);
        //((Activity)getContext()).finish();
    }

    public void resume(Context context) {
        //mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        //mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);      
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

前事休说 2025-01-03 11:46:42

我认为您可以使用 ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF Intent 操作,并且不必处理 onPause 和 onResume 的事情。所以像这样:

mReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (Intent.ACTION_SCREEN_OFF.equals(action)) {
        //deregister
    }
    else if (Intent.ACTION_SCREEN_ON.equals(action)) {
        //register
    }
}};

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(mReceiver, filter); 

filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);

并在 onDestroy() 方法中注销这个接收器


好吧,我想我找到了一个解决方案。 onSurfaceDestroyed 方法代码中的 thread.join 部分会以某种方式阻止活动。我对您的代码做了一些修改,这对我有用:

public MainGamePanel(Context context)
{
    ...

    //Also required for touch input.
    setFocusable(true);

    //start thread
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    //continue the thread
    synchronized (thread) {
        thread.pleaseWait = false;
        thread.notifyAll();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    //pause the thread
    synchronized (thread) {
        thread.pleaseWait = true;
    }
}

和线程类

public class MainThread extends Thread {

private SurfaceHolder surfaceHolder;
private MainGamePanel gamePanel;
private boolean running;
public boolean pleaseWait = true;
public void setRunning(boolean running) {
    this.running = running;
}

public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}

@Override
public void run() 
{
    Canvas canvas;
    while (running) {
        if(!pleaseWait) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    // update game state
                    this.gamePanel.update();

                    // draws the canvas on the panel
                    this.gamePanel.onDraw(canvas);
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally            
        }
        else {
            synchronized (this) {
                try {
                    wait();
                } catch (Exception e) { }
            }
        }
    }
}
}

并在 onDestroy 方法中销毁线程

I think you could use the ACTION_SCREEN_ON and ACTION_SCREEN_OFF Intent actions and you don't have to deal with onPause and onResume thing. So something like this:

mReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (Intent.ACTION_SCREEN_OFF.equals(action)) {
        //deregister
    }
    else if (Intent.ACTION_SCREEN_ON.equals(action)) {
        //register
    }
}};

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(mReceiver, filter); 

filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);

and deregister this receiver in the onDestroy() method


Ok I think I found a solution. The thread.join part in the onSurfaceDestroyed method code blocks the activity somehow. I made a few modifications on your code and this works for me:

public MainGamePanel(Context context)
{
    ...

    //Also required for touch input.
    setFocusable(true);

    //start thread
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    //continue the thread
    synchronized (thread) {
        thread.pleaseWait = false;
        thread.notifyAll();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    //pause the thread
    synchronized (thread) {
        thread.pleaseWait = true;
    }
}

And the thread class

public class MainThread extends Thread {

private SurfaceHolder surfaceHolder;
private MainGamePanel gamePanel;
private boolean running;
public boolean pleaseWait = true;
public void setRunning(boolean running) {
    this.running = running;
}

public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}

@Override
public void run() 
{
    Canvas canvas;
    while (running) {
        if(!pleaseWait) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    // update game state
                    this.gamePanel.update();

                    // draws the canvas on the panel
                    this.gamePanel.onDraw(canvas);
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally            
        }
        else {
            synchronized (this) {
                try {
                    wait();
                } catch (Exception e) { }
            }
        }
    }
}
}

And destroy the thread in the onDestroy method

沙沙粒小 2025-01-03 11:46:42

我认为,如果您使用 onCreate() 进行注册并使用 onDestroy() 进行注销,那将解决您的问题。因为当你的屏幕锁定时,你的 onDestory 不会被调用,这可能会解决你的问题。

I think, if you use onCreate() for register and onDestroy() to unregister, that ill solve your problem. Becoz your onDestory wont be called while your screen locked and that might solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文