Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified
Accepted Solution

Draining battery...need some advice

I have an application that runs in the background and runs every 15,30,60 min based off of what the user has it set for. I makes a quick call to a website gets a little bit of info and then closes the connection. I have a realtimeclocklistener setup to track how many minutes have gone by since the last update.

 

The problem is even when this is set to update every 60 min it just sits there and drains the battery life. Also the connection arrows in the top right will come up randomly even after it's done updating. I'm not sure if this is just conscience or if that isthe issue.

 

Here is the code I'm ruuning to get the data 

 

public void retrieveData(){ //Vars HttpConnection httpConn = null; OutputStream os = null; DataInputStream is = null; StringBuffer sb = null; String separator = "|"; int chr; int index; synchronized(this){ try{ httpConn = (HttpConnection)Connector.open(myURL, Connector.READ); httpConn.setRequestMethod(HttpConnection.GET); httpConn.setRequestProperty("Content-Type", "text/plain"); if(httpConn.getResponseCode() == HttpConnection.HTTP_OK){ //Vars os = httpConn.openOutputStream(); os.flush(); sb = new StringBuffer(); is = httpConn.openDataInputStream(); while ((chr = is.read()) != -1) { sb.append((char) chr); } String response = new String(sb.toString().getBytes(),"UTF-8"); Vector nodes = new Vector(); index = response.indexOf(separator); //Code while(index>=0) { nodes.addElement( response.substring(0, index) ); response = response.substring(index+separator.length()); index = response.indexOf(separator); } nodes.addElement( response ); String[] result = new String[ nodes.size() ]; if( nodes.size()>0 ) { for(int loop=0; loop<nodes.size(); loop++){ result[loop] = (String)nodes.elementAt(loop); } } runUpdate(); } } catch(IOException e){} //Null Values finally { if(is != null){ try { is.close(); is = null; } catch(IOException e){}} if(os != null){ try { os.close(); os = null; } catch(IOException e){}} if(sb != null){ sb = null; } separator = null; if(httpConn != null){ try { httpConn.close(); httpConn = null; } catch(IOException e){}} } } }

 

I looked at a lot of samples and others code online and I don't see anything wrong with my code.

 

Any help will be much appriciated.

 

Developer
Posts: 19,616
Registered: ‎07-14-2008
My Device: Not Specified

Re: Draining battery...need some advice

That all looks OK to me.  I'd be very surprised if this code drained the battery much, especially if it only invoked every 60 minutes.  Are you sure there is not something in your RealtimeClockListener processing?  Also does the Thread that runs this close down?  It would seem that you could drive this processing entirely off the RealtimeClockListener, just starting the Thread when you want it.  If you did this, then I think your application would have minimal impact on the battery. 
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Draining battery...need some advice


peter_strange wrote:
That all looks OK to me.  I'd be very surprised if this code drained the battery much, especially if it only invoked every 60 minutes.  Are you sure there is not something in your RealtimeClockListener processing?  Also does the Thread that runs this close down?  It would seem that you could drive this processing entirely off the RealtimeClockListener, just starting the Thread when you want it.  If you did this, then I think your application would have minimal impact on the battery. 

The way I have it setup is very minute it runs a quick check to see if the x amount of minutes that has passed is equal to the number of minutes that the user has. If it isn't then I have 1 variable increment and that is all that happens. If the variable is equal to the number the user has set I run that function above. I don't have the internet method in a thread, could that be where my problem lies? I can put that in a thread and have it run only when needed and then shutdown the thread until it's needed again, would that make a difference?

 

Thanks peter

New Developer
Posts: 13
Registered: ‎03-03-2009
My Device: Not Specified

Re: Draining battery...need some advice

Hi,

 

My guess is that there is something wrong in the timer code/how often does the update actually happens.  I'd log the time before opening the connection and after closing it and see if the updates happen as often as you expect them.

 

Regards,

Adrian

Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Draining battery...need some advice

After checking my code over and over again I found out what was causing the issue. It was a problem with 1 of the variables and it caused it to update every min rather then every 15,30,60 minutes like it should have. Man 1 simple variable caused so many headaches.

 

Thanks guys