简体   繁体   中英

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. 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. 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:

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

Also make sure to not re-register the listener in the onResume method.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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