Is it possible to use the accelerometer in a background service?

by John Smith » Thu, 10 Sep 2009 01:05:47 GMT


Sponsored Links
 It seems trivial to use GPS in a background service, but how can you
do the same with the accelerometer?

Everything I've tried seems to require a context, but a background
service doesn't have a context?

--~--~---------~--~----~------------~-------~--~----~



Is it possible to use the accelerometer in a background service?

by Mike Collins » Thu, 10 Sep 2009 01:43:01 GMT


 my service uses "getBaseContext()", seems to work just fine.  Don't
use it for
GPS but we do use it for many other things.

  mike




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Is it possible to use the accelerometer in a background service?

by dadical » Thu, 10 Sep 2009 02:46:05 GMT


 I register a listener to orientation sensor from within a background
service with no trouble.  Perhaps the following code may help.  These
methods are called from my service's onCreate and onDestroy methods:

        private void startMonitoring(){
                SensorManager lMgr = (SensorManager) getSystemService
(Context.SENSOR_SERVICE);

                try{
                        lMgr.unregisterListener(this);
                } catch(Exception e){
                        Log.d(ScreeblService.class.getSimpleName(), 
e.toString());
                }

                List<Sensor> lSensorList = lMgr.getSensorList
(Sensor.TYPE_ORIENTATION);
                if(lSensorList == null || lSensorList.size() == 0){
                        throw new RuntimeException("Orientation sensor not 
available!");
                }

                                // assumes that this class implements
SensorEventListener
                lMgr.registerListener( this, lSensorList.get(0),
SensorManager.SENSOR_DELAY_NORMAL );

        }

        private void stopMonitoring(){
                SensorManager lMgr = (SensorManager) getSystemService
(Context.SENSOR_SERVICE);

                try{
                        lMgr.unregisterListener(this);
                } catch(Exception e){
                        Log.d(ScreeblService.class.getSimpleName(), 
e.toString());
                }

        }





--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Don't understand how to use SurfaceView for my game, have it working with nomrla View.

Problems with using SurfaceView, I don't understand how to draw to it.
It has to be done manually? How?



package com.android.WWS;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;

public class WWS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
    }

    private static class GameView extends SurfaceView implements
OnKeyListener {
        private Paint mPaint = new Paint();
        private int x;
        private int y;

        public GameView(Context context) {
            super(context);
            x = 135;
            y = 303;
            setFocusable(true);
            requestFocus();
        }

        @Override
        public void onDraw(Canvas canvas) {
            Paint paint = mPaint;
            canvas.translate(10, 10);
            canvas.drawColor(Color.rgb(184,134,11));
            paint.setColor(Color.rgb(107,142,35));
            paint.setStrokeWidth(1);
            canvas.drawRect(x, y, x+30, y+7, paint);
            canvas.drawRect(x+10, y+7, x+20, y+27, paint);
            canvas.drawRect(x+5, y+27, x+25, y+32, paint);
        }

        public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        y -= 3;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                        x -= 3;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                        y += 3;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                        x += 3;
                }
                return true;
        }

    }

}


--~--~---------~--~----~------------~-------~--~----~

2. how to install android market and video player

Hi,

I am using beagle board, in which android being ported.

i have to play mp4 video files, on searching in the internet, i have found
that android market need to be installed to get videoplayer and many
applications.

how to get android market installed in my board.

Thanks and Regards,
HarishKumar.V

--~--~---------~--~----~------------~-------~--~----~

3. SoftHashMap implementation - For use as a Cache of thumbnails

4. enable ttyMSM2

5. Whole lotta garbage collecting going on.... How do I find out what is being collected?

6. SoftHashMap implementation - For use as a Cache of thumbnails

7. SoftHashMap implementation - For use as a Cache of thumbnails