Service restarting for no reason?

by DCheeseman » Thu, 11 Mar 2010 22:56:47 GMT

Sponsored Links
 hile debugging one of my apps (one that sits in the background and
logs accelerometer data), I noticed that it would randomly and without
any prompt reset itself. It would be running it's normal timer task
and suddenly it would stop, the create function would be called and
any running task would halt. Here's the trace:

03-11 07:50:34.574: VERBOSE/AccelerLogService(29277):
03-11 07:50:34.584: VERBOSE/AccelerLogService(29277): History Size:
03-11 07:50:35.064: VERBOSE/AccelerLogService(29277):
03-11 07:50:35.084: VERBOSE/AccelerLogService(29277): History Size:
03-11 07:50:35.544: VERBOSE/AccelerLogService(29277):
03-11 07:50:36.044: VERBOSE/AccelerLogService(29277): History Size:
03-11 07:50:36.484: VERBOSE/AccelerLogService(29277):
03-11 07:50:37.024: VERBOSE/AccelerLogService(29277): History Size:
03-11 07:50:38.994: VERBOSE/AccelerLogService(29277):
03-11 07:50:39.014: VERBOSE/AccelerLogService(29277): History Size:
03-11 07:51:03.984: VERBOSE/AccelerLogService(29395): CREATED
03-11 07:52:41.624: VERBOSE/AccelerLogService(29502): CREATED

And here's the service code:

public class AccelerLogService extends Service implements
SensorEventListener {
private static final String LOG_TAG = AccelerLogService.class
private static final String base_dir = "/sdcard/";
private static WakeLock wl;

private long startTime;
private volatile float[] lastData;
private static Queue<float[]> lastFiveMinutes;

private static BufferedWriter br;
private static Timer timer;
private static TimerTask tt;
float threshold = 0.0f;
int interval;

private boolean getAccelerometer() {
SensorManager sensorMgr = (SensorManager)
List<Sensor> sensorList = sensorMgr
boolean addedListener = sensorMgr.registerListener(
(SensorEventListener) this, sensorList.get(0),
if (addedListener)
Log.v(LOG_TAG, "Listener Added");
Log.v(LOG_TAG, "Listener Not Added");
return addedListener;

public void onCreate() {
if (timer == null)
timer = new Timer();
if (lastFiveMinutes == null)
lastFiveMinutes = new LinkedList<float[]>();

public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
if (intent.getExtras().containsKey("ms")) {
interval = intent.getExtras().getInt("ms");
} else {
interval = 5000;

Service restarting for no reason?

by Mark Murphy » Thu, 11 Mar 2010 23:53:34 GMT


Do not use TimerTask with an unbound Service. Use AlarmManager, so the
Service does not have to remain in memory all the time. 

WakeLocks have nothing to do with keeping services in memory. In
general, you don't *want* to keep services in memory any more than is

Mark Murphy (a Commons Guy)  | 

_Android Programming Tutorials_ Version 2.0 Available!


Sponsored Links

Other Threads

1. Bitmap GetPixel broken??

I'm trying to load a image, for the purpose of debugging I've made the
image a solid grey #494949.

I load it with the following code

inputstream =
level1_heightmap = BitmapFactory.decodeStream(inputstream);

Although when I run getpixel on the image it does not give a uniform
value, I put this debugging code in to confirm
for (int i=0;i<100;i++)
                        Log.d("Debug", "COLOR"+level1_heightmap.getPixel((int)

Which outputs the following colors
COLOR-11908790 and COLOR 11383470

Considering the image is a solid grey, shouldn't it just be outputting
one number? Is there 2 levels of loading for BitmapFactory and how do
I ensure that the image is fully loaded before working with it?


2. How to programmatically use "include" on xml

So I read the xml tricks 2 from, and it worked for me.
But I need to programmatically include 1 xml into another. addview returns

07-12 01:01:18.429: ERROR/AndroidRuntime(267):
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
07-12 01:01:18.429: ERROR/AndroidRuntime(267):     at

((ViewGroup) placeHolderProductList).addView(productList,
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)) ;

Please help.


3. Difference between SQLite on HTC Hero (1.5) and Nexus One (FR72)?

4. Perkenalan dengan si #robotijo murmer

5. Selesai update f/w dan rooting gak bisa konek 3

6. Continuously scrolling ListActivity

7. Configuring automated build scripts