0

My Android app needs to be constantly receiving via USB serial in the background of my app, while sending information via USB serial only happens on certain functions. When we send and receive I am always sending a packet of X bytes every time. I understand how Android USB API works, the thing that I am having trouble with is how would I organize this? Would I use a thread for receiving only and the rest as functions, or for the whole USB connection/sending and receiving all together is in a thread? The main activity is called "Homescreen.java" and here is how I have it organized so far.

public class HomeScreen extends Activity implements OnTouchListener, Runnable{
onCreate() { }
onResume() { }
onStart() { }
onDestroy() { }
run() { }
}

Note: The reason there is no onPause is because this app is a fullscreen widget and should never be closed.

Another question: If I was to make a thread would I have to make it extend from Homescreen.java? And what of Context? Can I just import it? (Not very keen on Context object)

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
Dogz1
  • 70
  • 2
  • 9

2 Answers2

0

this is more of design choice, for instance if you want one background thread to handle the data from USB

public class test extends Activity{
Thread t;
runT= true;

public void onCreate(Bundle b)
{
    ..........
    ..........
    t = new Thread(new Runnable() {
        @Override
        public void run()
        {
            while(runT)
            {
                //call data read or send functions here you can add condtion to sleep the thread as well
            }
        }
    });
    t.start();
}
}

When you are ending the activity simply set runT to false, which will stop the thread.

You can also have a thread pool and use theads accordingly.

If this is not happening frequently you can start an Asynctask everytime you want to send data.

Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • Thanks for the help, when I'm creating a new activity i noticed that in the usb api I need to reference Context. Can i just import android.content.Context;? Or is there an extra step in this to make sure im referencing the right variable? – Dogz1 Jun 01 '12 at 22:09
  • @dogz1 I would recommend that you read up on Android basics. There are plenty of resources online such as Android documentation. There is a series of good video tutorials from Marakana. Just google Marakana android for java developers. Marko explains these concepts very nicely. This stack post gives more info.about context http://stackoverflow.com/questions/3572463/what-is-context-in-android if the threading issue has been resolved mark it so – Orlymee Jun 01 '12 at 22:26
0

You can look at AsyncTask. It is a special thread implementation for Android that should keep things simpler. If you are not doing any threaded "heavy lifting" I would recommend going with AsyncTask. You simply write an inner class inside your HomeScreen class, write your logic and call it from your Activity (for example from within onCreate()).

You could try getBaseContext() from within the Activity - I guess this will get you the relevant Context.

Cheers

roysch
  • 773
  • 1
  • 7
  • 22