1

I know this issue has been running for a while and that it not recommended to want to achieve this but I need my service sensors to work even when the phone is on standby mode.

So far here is what I've got :

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);
        }

}

But this is not working, though it depends of the phone, is there something I'm not doing right? Or not doing at all?

Edit 1: It does work with SCREEN_DIM_WAKE_LOCK wich is an awfull workaround...

4

0 回答 0