I have a small data collection app which has a single button to start and stop a service.
The service in turn starts a new thread. Which collects data about voltage changes whenever there is a broadcast about it.
Data is temporarily stored in a ArrayList, and every 10 seconds the all the data stored in arrayList is dumped into a database.
App is running fine for 10-20 min but after 20 min app automatically stops. Some times service is still running and some times service is also killed.
Can you guys tell me what could be the problem here.
Things I think might relate to this problem are:
1.> I am opening and closing database every 10 sec. Should I change the design to close database only when the whole app is finished.
2.> I also took a wakelock, but that didnt make any difference. Should I take wakelock for my service also ?
3.> And lastly how can I come to know about the error because of which my app stopped( I can not connect to USB to know that coz I want battery to be in draining mode)
Update:
The logcat error is as follows:
E/AndroidRuntime( 9770): FATAL EXCEPTION: Timer-0
E/AndroidRuntime( 9770): java.util.ConcurrentModificationException
E/AndroidRuntime( 9770): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
E/AndroidRuntime( 9770): at com.amazon.hsyal.services.LoggerThread$2.run(LoggerThread.java:78)
E/AndroidRuntime( 9770): at java.util.Timer$TimerImpl.run(Timer.java:284)
W/ActivityManager( 236): Force finishing activity com.amazon.hsyal/.ui.VoltageSODLoggerActivity
I/ActivityManager( 236): No longer want com.amazon.dcp:OTAService (pid 12299): hidden #16
I/WindowManager( 236): WIN DEATH: Window{417dcd20 com.amazon.hsyal/com.amazon.hsyal.ui.VoltageSODLoggerActivity paused=true}
I/UsageStats( 236): No package stats for pkg:com.amazon.kindle.otter
I/ActivityManager( 236): Process com.amazon.hsyal (pid 9770) has died.
W/ActivityManager( 236): Scheduling restart of crashed service com.amazon.hsyal/.services.LoggerService in 5000ms
I/ActivityManager( 236): Start proc com.amazon.kindle for broadcast com.amazon.kindle/.PrimeAppReceiver: pid=13075 uid=32022 gids={3003, 1015}
W/ActivityManager( 236): Activity pause timeout for ActivityRecord{4173eb10 com.amazon.kindle.otter/.Launcher}
I/ActivityManager( 236): Start proc com.android.settings for broadcast com.android.settings/.TopWindowChangereceiver: pid=13111 uid=1000 gids={1015, 3002, 3001, 3003}
I/ActivityManager( 236): Start proc com.amazon.hsyal for service com.amazon.hsyal/.services.LoggerService: pid=13138 uid=10051 gids={}
W/ActivityManager( 236): Activity destroy timeout for ActivityRecord{41750838 com.amazon.hsyal/.ui.VoltageSODLoggerActivity}
and the corresponding Thread code is as follows:
public void run() {
// TODO Auto-generated method stub
dbInstance = new DbClass(ctx);
Log.d("Debuglogger","Came inside thread");
ctx.registerReceiver(this.voltageReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(threadStatus == false){
return;
}
if(!DataClass.dataList.isEmpty()){
dbInstance.open();
Iterator<DataClass> itr = DataClass.dataList.iterator();
DataClass a_temp;
while(itr.hasNext()){
a_temp = itr.next();
dbInstance.createEntry(a_temp.getVoltage(), a_temp.getCurrent(), a_temp.getBattery_level(),
a_temp.getTime(), a_temp.getRtime());
}
dbInstance.close();
DataClass.dataList.clear();
Log.d("db-error", "Data written to database and list cleared !");
}
}}, 0, UPDATE_INTERVAL);
}
As you can see from the stack trace, the error seems to be
java.util.ConcurrentModificationExceptionas you are accessing theArrayListfrom multiple threads. You can take a look at How to Avoid ConcurrentModificationException when using an Iterator, the relevant part of which is as below:There is not so much of an overhead to open and close the database every 10s. Think of this only if you have a performance issue.
Does not seem to be related to the particular issue.
Hope you have already understood how this can be done. Just to reiterate, you can obtain logcat output even after the application has stopped – either through DDMS or using the adb command :
adb logcat -d > log.txt