Android:GPS/传感器记录服务

发布于 2024-10-07 18:35:45 字数 97 浏览 0 评论 0原文

我正在寻找实现定期(每 10-60 秒)记录 GPS 或其他传感器值的服务的最佳实践。当手机进入睡眠状态时,该服务应该处理待机模式。

非常感谢任何帮助,即使是伪代码!

I m looking for the best practice to implement a service for logging gps- or other sensor-values periodically (every 10-60 sec). The service should deal with the standby mode, when the phone goes asleep.

Any help, even pseudo-code, is very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2024-10-14 18:35:45

看起来即使设备可能进入睡眠状态,也不可能让方向传感器持续工作几个小时(请参阅http://code.google.com/p/android/issues/detail?id=3708#makechanges

一旦显示屏关闭,传感器也会执行类似操作... :(

我现在实现了一个唤醒锁(需要许可)

<uses-permission android:name="android.permission.WAKE_LOCK" /> 

以及一个计时器和一个广播接收器,它将再次打开显示器。这对于电池寿命来说当然是疯狂的,但到目前为止我没有找到其他方法。

这是我在服务中的 onCreate 方法:

@Override
public void onCreate() {
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
CONTEXT = this;
PowerManager pm = (PowerManager) CONTEXT.getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "my tag");
mWakeLock.acquire();
Log.d(TAG, "Wakelock acquired");
// register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
}

这是服务的 onStart 方法:

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStart");
boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {
                Log.d(TAG, "Screen is on");
            } else {
             Log.d(TAG, "Screen is off");
         Timer timer = new Timer("DigitalClock");
     timer.schedule(new TimerTask() {
        @Override
        public void run() {
         Log.d(TAG,"Waiting 1 sec for switching the screen back on again...");
     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
     mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
         "my tag");
     mWakeLock.acquire();
        }
     }, 1000);
             mWakeLock.acquire();
                }
}

这是 BroadcastReceiver 类:

public class ScreenReceiver 扩展 BroadcastReceiver {
     私人布尔屏幕关闭;
     @覆盖
     公共无效onReceive(上下文上下文,意图意图){
             if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                     屏幕关闭=真;
             } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                     屏幕关闭=假;
             }
             意图 i = new Intent(context, MyService.class);
             i.putExtra("screen_state", screenOff);
             上下文.startService(i);
     }

}

适用于通过 USB 从调试器中拔出的设备(我首先遇到了这个问题)。

如果您有更好的解决方案,或者 2.3 中是否有修复,请告诉我。谢谢!

It looks like it is impossible to let the orientation sensors work constantly for hours even though the device may fall asleep (refer to http://code.google.com/p/android/issues/detail?id=3708#makechanges

As soon as the display goes off, the sensors will do alike... :(

I now implemented a wakelock (needs permission)

<uses-permission android:name="android.permission.WAKE_LOCK" /> 

in conjunction with a timer and a broadcastreciever that will turn the display back on again. This is of course crazy for battery life but I found no other way so far.

This is my onCreate method in the service:

@Override
public void onCreate() {
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
CONTEXT = this;
PowerManager pm = (PowerManager) CONTEXT.getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "my tag");
mWakeLock.acquire();
Log.d(TAG, "Wakelock acquired");
// register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
}

this the onStart method of the service:

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStart");
boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {
                Log.d(TAG, "Screen is on");
            } else {
             Log.d(TAG, "Screen is off");
         Timer timer = new Timer("DigitalClock");
     timer.schedule(new TimerTask() {
        @Override
        public void run() {
         Log.d(TAG,"Waiting 1 sec for switching the screen back on again...");
     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
     mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
         "my tag");
     mWakeLock.acquire();
        }
     }, 1000);
             mWakeLock.acquire();
                }
}

and this is the BroadcastReceiver class:

public class ScreenReceiver extends BroadcastReceiver {
     private boolean screenOff;
     @Override
     public void onReceive(Context context, Intent intent) {
             if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                     screenOff = true;
             } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                     screenOff = false;
             }
             Intent i = new Intent(context, MyService.class);
             i.putExtra("screen_state", screenOff);
             context.startService(i);
     }

}

This workaround will also do with a device UN-plugged from the debugger via USB (I first had an issue with this).

Please let me know if you have a better solution, or if there is fix in 2.3. Thanx!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文