1

この問題はしばらくの間発生しており、これを達成することはお勧めできませんが、電話がスタンバイ モードの場合でもサービス センサーが動作する必要があります。

これまでのところ、私が持っているものは次のとおりです。

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 で動作します...

4

0 に答える 0