1

I am pretty new to Android programming but got some experience in other languages. I want to create an APP which is principle working like this.

enter image description here

  1. The APP is a process which is asking my Web-/Database-Server every 10 seconds if there is an event to execute.

  2. The Web-/Database-Server answers with an event id or even a function name.

  3. The APP opens a new thread which executes the event with the id or even better directly the function name.

My Questions are:

  1. Is this perfomant? Or can this chrash pretty easily?
  2. Is it limited to 2 threads within the process or can I even open every time a new thread for the function which I want to execute? Maybe because of the other function is still running?
  3. How can I execute a function with the return value? For example

    InputStream in = response.getEntity().getContent(); //Get the data in the entity

    public in(void) { // execute a function which got´s the same name as the variable "in" }

The result should be: The one thread is asking every 10 seconds my Web-/Database-Server if there is an event. The event gets executed within a thread and this is working parallel (at the same time without crashing or even getting stucked).

Sample Code of these Threads would be appreciated.

My code till yet:

public class service extends Service { private static final String TAG = "MyService";

@Override
public IBinder onBind(Intent intent) 
{
    return null;
}

public void onDestroy() 
{
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
public int onStartCommand(Intent intent, int flags, int startid)
{

    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

    Thread thread = new Thread(new Runnable()
    {
      @Override
      public void run()
      {
         new Timer().scheduleAtFixedRate(new TimerTask() 
         {
          @Override
            public void run() 
          {
                     System.out.println("Send GO!");
                     Device dev = new Device();

                     dev.getDeviceId(getBaseContext());
                     dev.getEmail(getBaseContext());

                     dev.sendDeviceItems();
                   }
               }, 0, 10000);//put here time 1000 milliseconds=1 second
          }
          });

    thread.start();   
    return Service.START_STICKY;
   } }
2
  • Polling (every 10 seconds) is not a good solution because your app won't be able to run disconnected from the server. You should revist your requirements and possibly a port solution that the server can ping when it has a new message for your app to process. Side note: I invented the word performant! I've been using it for years. Commented Sep 30, 2014 at 18:40
  • Also a nice solution. I was also thinking of this but is it possible that the phone if it won´t receive a message within a time range, get´s into a kind of sleep mode? And if the phone is permanent listening, is this really effective or also a waste of battery? Commented Sep 30, 2014 at 18:49

1 Answer 1

2
  1. Is this perfomant? Or can this chrash pretty easily?
    • Yes it will be performance hit. Asking for api every 10sec will drain your battery also.
    • With proper exception handling it won't crash pretty easily.

Is it limited to 2 threads within the process or can I even open every time a new thread for the function which I want to execute? Maybe because of the other function is still running?

  • You can run as many threads you want but it will be difficult to maintain code in that
    case . You can use VOLLY or OKHTTP for network related things they will handle you thread part also.

How can I execute a function with the return value? For example

InputStream in = response.getEntity().getContent(); //Get the data in the entity

public in(void) { // execute a function which got´s the same name as the variable "in" }

  • For calling method at run time with Method name you can just use of REFLECTION in java. This will fulfil your need.
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Suhail Mehta! Thanks a lot for this explanation! :) I will try to get through this stuff you posted :) really thanks! :) Other tips or even hints are of course still appreciated!
Found many usefull things within this post, especially volley.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.