1

I’m new to android programming so my question is a basic one. I need to make application with the background process that constantly sends some data (getting it from BT). Idea is that communication should work even if app is in background or closed.

As I suppose I need to extend IntentService than to create working thread. Than if when I need to send commands to the working thread I should start intent service with other parameters in intent and there to send messages to the working thread.

I tried to do this and I faced problems with sending message from onHandleIntent().

So my questions are: what is proper model for the app with such tasks? Probably you know where examples might be seen? It seems like I do something very common, but I couldn’t find examples to answer my questions.

UPD: I made working prototype. Is this the best way to implement IntentService and worker thread?

public class WorkIntentService extends IntentService {

public static final String TAG = WorkerThread.class.getSimpleName();

public static final String ACTION_START = TAG + "[action_start]";
public static final String ACTION_STOP = TAG + "[action_stop]";

public static final String MSG_STOP = TAG + "[message_stop]";

public WorkIntentService() {
    super("WorkIntentService");
    workerThread = null;
}

WorkerThread workerThread;

@Override
protected void onHandleIntent(Intent intent) {

    if (intent != null) {

        String sAction = intent.getAction();

        if(sAction == ACTION_START){

            if(workerThread == null) {
                workerThread = new WorkerThread();
                workerThread.start();
            }

        }
        else  if(sAction == ACTION_STOP){

            if(workerThread == null) {

                LocalBroadcastManager.getInstance(null)
                        .sendBroadcast(new Intent(MSG_STOP));
            }

        }

    }
}

private static int nCounter = 0;
private static long TIMEOUT = 1000;

private class WorkerThread extends Thread{

    long lCounter;
    public boolean bRun;

    public void run() {

        lCounter = System.currentTimeMillis();
        bRun = true;

        LocalBroadcastManager.getInstance(WorkIntentService.this).registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {

                            bRun = false;
                    }
                },  new IntentFilter(MSG_STOP));

        while (bRun){

            if( System.currentTimeMillis() - lCounter > TIMEOUT ){
                lCounter = System.currentTimeMillis();
                Log.i(TAG, "next #" + Integer.toString(nCounter++));
            }

        }

    }

}

}

Yura
  • 969
  • 14
  • 33
  • `... and I faced problems ...` - then post your code and the problems you faced – pskink Aug 13 '17 at 08:37
  • You have to create a `service` that runs in background and do the BT communication. If you want to run the service on boot: https://stackoverflow.com/questions/7690350/android-start-service-on-boot. Consider this "answer" as a tip, if you'll include your code with a specific question you'll get a proper answer to solve your issue. – anemomylos Aug 13 '17 at 09:52
  • Thank you! I'll try to implement this at the evening – Yura Aug 13 '17 at 10:23

0 Answers0