3

I am fairly new to android programming and am pretty proficient in java, but have never done threading in java.

The project that I am currently working on I inherited from another programmer (who no longer works at the company). The code that I received was not amazing (2 giant classes, each 1500+ lines). I have spent the better part of a month reorganizing, cleaning it up etc.

I am looking to add a service or a background-thread because this is how it should have been organized in the first place. Being an android novice I read through the dev docs, looked at examples and various tutorials. What are some considerations that I should take into account and how should I decide which is better for my project?

Edit:

The service that I am looking to implement needs to monitor information coming in on the usb input as well as "asking" the device for its software version, for example. Currently if the user starts another activity then the USBManager is stopped. This is not ideal as some information could have been missed when the first activity was paused. If I am going to use a service what is the best way to communicate with it?

2 Answers2

1

First of all, please keep in mind that 'giant god' classes are not always atrocious in Android.

Since there's no way to identify what Service you are talking about, I think you need to get a better understanding on whether should a Service or a AsyncTask run.

Service

A Service is a part of your Application that has no UI. It may be called by a UI(Activity) to be started, or may be started by any other component of your Application. When developing, you have the freedom to place it on a different thread, or even run it in a different Task or Process. This allows you to ultimately separate it from your UI. Additionally, you may start the Service to run independently (startService) or bind your activity to it (bindService) depending upon your needs. By using custom Handlers, you can set callbacks to update the UI with your progress. A Service does not necessarily end if a User changes Activities, but may be ended at ANY time by the OS. [1]

AsyncTask

A AsyncTask is always instantiated from the UI thread. It only allows specific callbacks, but simplifies the process of multi-threading for the purposes of relatively short transactions (as compared to dedicated separate threaded services) that are inherently tied to actions performed by an Activity. Whenever a User changes Activities, the AsyncTask is put on "pause" and may even die because there is no UI thread for your Activity any longer. [2]

Thus, sometimes you will handle situations where you won't have to use neither.

Community
  • 1
  • 1
Machado
  • 14,105
  • 13
  • 56
  • 97
  • Based on your information I edited my original question. Just wondering if you have any further insights? – Katherine B Jul 13 '15 at 19:05
  • Dealing with USB events might generate a few crashes if the cable state is: not connected. Make sure you filter all these events under your Service so your application won't go down due to connection issues. – Machado Jul 13 '15 at 20:56
0

Considering the information so far, you should describe what kind of event you are associating with the USB.

This should be covered first if you just need to perceive wheter or not there is something plugged in.

Then, as Machado wrote, classes in Android are significantly different from Java. Just to point out, in Dalvik (the "Android Java"), one should always go for least operations necessary to complete the desired effect, so the whole concept of getters/setters are discouraged, as well as other programming "best practices" do not apply in this enviroment.

From what I could understand in your question, reffer to Broadcasts associated with USB, then ask for the permissions necessary to do those actions.

Finally, regarding the service: realise that all operations are "energy" oriented in the Dalvik, and thus, only do processing if its mandatory.

If the USB device needs to keep communicating, then you should do a service, and keep the interactions service <> usb device. But, if the device can keep silent, use events to listen to the interactions, and perform your operations as needed.

Also, consider the "Lifecycle" on applications, try to keep OO directives with the minimal ammount of operations as possible, and the minnimum processing necessary, to prevent ANRs (application not responding)

Bonatti
  • 2,778
  • 5
  • 23
  • 42
  • There are a bunch of things that I know that I still need to learn! Do you know of any good places where the differences in java and android best practices are talked about? – Katherine B Jul 14 '15 at 15:27
  • If you have the time and effort, the [Google](http://developer.android.com/develop/index.html) pages are the best to start. If you feel like its too basic, then skip ahead sections... the important part so far, is the IDE training, and perhaps the Code Samples. – Bonatti Jul 15 '15 at 11:04