传感器处于待机模式

时间:2012-10-17 13:32:57

标签: android broadcastreceiver android-sensors android-wake-lock

我知道这个问题已经运行了一段时间,并且不建议想要实现这一点,但即使手机处于待机模式,我也需要我的服务传感器才能工作。

到目前为止,我所拥有的是:

public class SensorOrientation extends Service implements SensorEventListener{


    private     SensorManager   mSensorManager = null;
    private     Sensor          orientation;
    private     int             dureeDetection;
    private     int             angle;
    private     int             angleOrientation;

    public      static final int SCREEN_OFF_RECEIVER_DELAY = 500;

    public      static final String TAG         = SensorOrientation.class.getName();
    private     WakeLock            mWakeLock   = null;



    private static final String LOG_TAG = "SensorsOrientation";
    float x, y, z;
    protected BroadcastReceiver     mreceiver;

        public int onStartCommand(final Intent intent, final int flags,
            final int startId) {

                startForeground(1, new Notification());
                registerListener();
                mWakeLock.acquire();

                return START_STICKY;

    }

    private void registerListener() {
          mSensorManager.registerListener(this,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
          SensorManager.SENSOR_DELAY_UI);
        }

    private void unregisterListener() {
        mSensorManager.unregisterListener(this);
    }

     public BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.i(TAG, "onReceive("+intent+")");

                if (!intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                    return;
                }

                Runnable runnable = new Runnable() {
                    public void run() {
                        Log.i(TAG, "Runnable executing.");
                        unregisterListener();
                        registerListener();
                    }


                };

                new Handler().postDelayed(runnable, SCREEN_OFF_RECEIVER_DELAY);
            }
        };

    public void onSensorChanged(SensorEvent event) {

            // update only when your are in the right case:
            Log.i(TAG, "onSensorChanged().");

            if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                // the azimuts
                x = event.values[0];
                // the pitch
                y = event.values[1];
                // the roll
                z = event.values[2];

                angleOrientation = -1 * ((int)y);

                if ((angle == 90 && angleOrientation <= 0)  || 
                (angle == 60 && angleOrientation <= 30)     || 
                (angle == 40 && angleOrientation <= 50)     ||
                (angle == 30 && angleOrientation <= 60 )) {

                    Toast.makeText(SensorOrientation.this,
                            "perte de verticalité",
                            Toast.LENGTH_SHORT).show();

                }

                //Log.d(LOG_TAG, "Sensor's values (" + ((int) x) + "," + ((int) y) + "," + ((int) z) + ")");
            }
        }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

     @Override
        public void onCreate() {
            super.onCreate();

            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

            PowerManager manager =
            (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

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

            DBAdapter db    =   new DBAdapter(getApplicationContext());
            Cursor options  =   db.selectVerticalite();

            dureeDetection  =   options.getInt(options.getColumnIndex("duree_detection_verticalite"));
            angle           =   options.getInt(options.getColumnIndex("angle_detection"));

            options.close();
        }

     @Override
        public void onDestroy() {
            unregisterReceiver(mReceiver);
            unregisterListener();
            mWakeLock.release();
            stopForeground(true);
        }

}

但这不起作用,虽然这取决于手机,有什么我做得不对吗?或者根本不做?

编辑1:它适用于SCREEN_DIM_WAKE_LOCK,这是一个非常糟糕的解决方法...

0 个答案:

没有答案