How to get sensor data in service when screen is turned off?

Maaz Asif

I am trying to create an application that tracks the orientation of the device for a certain amount of time using a service. when the orientation changes, the device makes a sound. This is working perfectly as long as the device is on. As soon as i lock the device or the screen turns off, i dont hear the sounds (which i want to).

My service code is

public class RakatTrackingService extends Service implements SensorEventListener {
private SharedPreferences sharedPref;
private long[] sessionComposition = new long[4];
private String position="none", lastPosition ="none";
private SensorManager sensorManager;
private Sensor accelerometer;
private PowerManager.WakeLock wakeLock;

public RakatTrackingService()
{

}
public RakatTrackingService(long[] sessionComposition) {
    this.sessionComposition = sessionComposition;


}

@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences sharedPref = getSharedPreferences("rakatData",Context.MODE_PRIVATE);

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null)
    {
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        Toast.makeText(this, "sensor registered", Toast.LENGTH_LONG).show();
    }
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onSensorChanged(SensorEvent event) {

    lastPosition = position;
    if(Math.abs(event.values[0]) > Math.abs(event.values[1]) && Math.abs(event.values[0]) > Math.abs(event.values[2]))
    {
        position="horizontal-side";
    }
    if(Math.abs(event.values[1]) > Math.abs(event.values[0]) && Math.abs(event.values[1]) > Math.abs(event.values[2]))
    {
        position="vertical";
    }
    if(Math.abs(event.values[2]) > Math.abs(event.values[0]) && Math.abs(event.values[2]) > Math.abs(event.values[1]))
    {
        position="horizontal";
    }


    System.out.println(position);

    if(!lastPosition.equalsIgnoreCase(position))
    {
        MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
        mp.start();
        mp.setLooping(false);
    }
}

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

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    PowerManager mgr = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
    return START_STICKY;

}

@Override
public void onDestroy() {
    super.onDestroy();
    wakeLock.release();
}
}

Please help me resolve this issue.

webo80

Simply you can't. Sensor listeners doesn't work then your screen is off. You need the app to be active for using it. You can try to acquire a PARTIAL_WAKE_LOCK or a SCREEN_DIM_WAKE_LOCK

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why does Ubuntu boot from flash with the screen turned off?

分類Dev

How to find out what turned off the computer?

分類Dev

The screen fades when it is turned on, but nothing can be seen on it

分類Dev

How to get a full screenshot of a page that goes off screen?

分類Dev

How to remove the FC layer off of a fine turned model keras

分類Dev

How to turn off screen with PowerShell?

分類Dev

If I have location services turned on then how can my app get killed when it enters background mode?

分類Dev

UISearchBar of UISearchController moves off screen when active

分類Dev

footer scrolls off screen when used in CoordinatorLayout

分類Dev

notebook turned off while upgrading

分類Dev

How can -Wgnu-zero-variadic-macro-arguments warning be turned off with Clang?

分類Dev

Turn screen off when inactive for time period in RHEL and Debian

分類Dev

android: app stops and then crashes when the screen goes off

分類Dev

Android Thread: App hangs when screen turns off

分類Dev

SCRIPT1030: Conditional compilation is turned off

分類Dev

Would there be any problems with DEP turned off?

分類Dev

DHCPD logs show PC's requesting IP addresses from router when they are turned off. Are our log files incorrect?

分類Dev

How to get my systemd service restarted when its dependency is upgraded

分類Dev

Turn off AwaysOn in Azure Function to get a free App Service Plan?

分類Dev

How to manipulate a window which is in Off Screen status by UI Automation

分類Dev

how to stop objects from being dragged off screen

分類Dev

How to get a specific data value in a data table when clicked - VueJS?

分類Dev

Get a notification & dialog when the screen is locked

分類Dev

How do you get pygame to give warning when player touches side of screen?

分類Dev

How to make new window get the focus when Emacs in full-screen mode in Xfce4?

分類Dev

How to persist Sensor telemetry data into cold storage such as big data storage or cosmosdb

分類Dev

On Android, can I register for a callback that tells me if Bluetooth is turned on or off?

分類Dev

Is it necessary to use the "volatile" qualifier even in case the GCC optimisations are turned off?

分類Dev

Is it possible to find the MAC adress of a turned off computer somwhere in my LAN?

Related 関連記事

  1. 1

    Why does Ubuntu boot from flash with the screen turned off?

  2. 2

    How to find out what turned off the computer?

  3. 3

    The screen fades when it is turned on, but nothing can be seen on it

  4. 4

    How to get a full screenshot of a page that goes off screen?

  5. 5

    How to remove the FC layer off of a fine turned model keras

  6. 6

    How to turn off screen with PowerShell?

  7. 7

    If I have location services turned on then how can my app get killed when it enters background mode?

  8. 8

    UISearchBar of UISearchController moves off screen when active

  9. 9

    footer scrolls off screen when used in CoordinatorLayout

  10. 10

    notebook turned off while upgrading

  11. 11

    How can -Wgnu-zero-variadic-macro-arguments warning be turned off with Clang?

  12. 12

    Turn screen off when inactive for time period in RHEL and Debian

  13. 13

    android: app stops and then crashes when the screen goes off

  14. 14

    Android Thread: App hangs when screen turns off

  15. 15

    SCRIPT1030: Conditional compilation is turned off

  16. 16

    Would there be any problems with DEP turned off?

  17. 17

    DHCPD logs show PC's requesting IP addresses from router when they are turned off. Are our log files incorrect?

  18. 18

    How to get my systemd service restarted when its dependency is upgraded

  19. 19

    Turn off AwaysOn in Azure Function to get a free App Service Plan?

  20. 20

    How to manipulate a window which is in Off Screen status by UI Automation

  21. 21

    how to stop objects from being dragged off screen

  22. 22

    How to get a specific data value in a data table when clicked - VueJS?

  23. 23

    Get a notification & dialog when the screen is locked

  24. 24

    How do you get pygame to give warning when player touches side of screen?

  25. 25

    How to make new window get the focus when Emacs in full-screen mode in Xfce4?

  26. 26

    How to persist Sensor telemetry data into cold storage such as big data storage or cosmosdb

  27. 27

    On Android, can I register for a callback that tells me if Bluetooth is turned on or off?

  28. 28

    Is it necessary to use the "volatile" qualifier even in case the GCC optimisations are turned off?

  29. 29

    Is it possible to find the MAC adress of a turned off computer somwhere in my LAN?

ホットタグ

アーカイブ