Impossibility to change the rate of the accelerometer

后端 未结 4 2034
萌比男神i
萌比男神i 2021-01-06 12:13

I have to collect accelerometer data from my Android phone, so I have made a little program to do that.

After some tests, I have found that my Nexus S accelerometer\

4条回答
  •  青春惊慌失措
    2021-01-06 13:11

    Unfortunately as suggested by other answers, the delay (or sampling rate) that you set is only a suggestion (for the system) of the minimum your application requires however, this can change drastically depending on other applications running or your device entering on power saving modes (turning off cpu etc). One way I was able to get a somewhat good sampling rate was by recording my data from a service, making the service a foreground service and having a power lock with the flag PARTIAL_WAKE_LOCK.

    I put the code in this gist https://gist.github.com/julian-ramos/6ee7ad8a74ee4b442530

    That gist has way more code than you need but just pay attention to the next two methods

    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    //        //Power management
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
            wl.acquire();
    .....}
    

    The above code prevents the OS to turn off the CPU

    Now to make this service a foreground service

    NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(getApplicationContext())
                            .setContentTitle("Title")
                            .setContentText("hola")
                            .setContentIntent(viewPendingIntent);
    
    
    
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    
            not=notificationBuilder.build();
            notificationManager.notify(notificationId,not );
            startForeground(notificationId, not);
    

    The above code makes the service a priority for the OS unlike a normal service. Examples of a foreground service are music players for instance.

    With this code I have been able to get the sampling rate I need even after the watch turn's off the screen automatically.

提交回复
热议问题