简体   繁体   English

具有前台活动的Android服务

[英]Android Service with foreground activity

I created an application that is taking accelerometer data(x,y and z values) and showing them in a textfield. 我创建了一个使用加速度计数据(x,y和z值)并将其显示在文本字段中的应用程序。 I would like to create a service that will run in background so that the accelerometer runs even if I navigate from the app.Its like this there will be a button to start the service; 我想创建一个将在后台运行的服务,以便即使我从应用程序导航也可以运行加速度计。这样,将有一个按钮可以启动该服务; so when I click the button the app will go in background and run. 因此,当我单击该按钮时,该应用将在后台运行。 One thing is that while running in the foreground I was writing the acceleromter data in a csv file so I need the same thing happened in the background also. 一件事是,当我在前台运行时,我正在将加速器数据写入csv文件中,因此我也需要在后台执行相同的操作。 Is that possible? 那可能吗? Can anyone help me providing any simple example or links ?? 谁能帮助我提供任何简单的示例或链接?

A simple fix could be to not unregister the listener in the onPause() method: 一个简单的解决方法是不要在onPause()方法中取消注册侦听器:

@Override
protected void onPause(){
    super.onPause();
    //sensorManager.unregisterListener(this);
}

Also make sure to not re-register the listener in the onResume method. 还要确保不要在onResume方法中重新注册侦听器。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM