Managing a connection to the mysql through a servi

2019-09-03 09:23发布

问题:

I got a service that generate the phone location. I would like to send this string into my db but it's impossible through the service since I get a connection on main thread exception. How should I do that?I would like to send the location every x time(probably a min).

I know how to connect to a mysql db and all around but I just don't know how to manage this system, thanks.

回答1:

That has nothing to do with the service itself.

The problem is that your operations are being executed on the UI thread, which is effectively stopping the UI from doing anything else.

The thread that calls the service life-cycle callbacks (onStartCommand(), onBind(), etc) is the UI thread. You just have to fire it on an executor service, or a new thread, or depending on what you're implementing a IntentService might be a handy class to use.