3
\$\begingroup\$

I'm a little puzzled about a procedure inside my Android app. It's actually working but it looks like bad practice and probably it lowers performances.

Short explanation: I'm running a separate Thread inside a Service. Inside the thread I fetch a list of files and the main purpose of the thread is to query the file to a different service that reads the file and answers back to the thread with the file content through a BroadcastReceiver, then the thread sends that content (with some edits, of course) to a different service that write it to the same file and waiting for it to finish, using a second BroadcastReceiver. Doing the same for all the files, one by one.

private class TagProcess extends Thread {

        private boolean tagSaved = false;

        TagProcess(Context context) {
            registerReceiver(mReadReceiver, new IntentFilter(TagReaderService.BROADCAST_CHANNEL_NAME));
            registerReceiver(mWriteReceiver, new IntentFilter(TagWriterService.BROADCAST_CHANNEL_NAME));
        }

        @Override
        public void run() {
            int result;
            do {
                result = scanNextSong();

                if(result == 1) {
                    while (!tagSaved) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } while ( result != -1);

            processTermination();
        }

        private int scanNextSong() {
            if (/* SOME RROOR*/) {
                return -1;
            }

            startService(/* READ SERVICE */);

            tagSaved = false;

            return 1;
        }

        private void processTermination() {
            stopSelf();
        }

        private BroadcastReceiver mWriteReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // Done writing

                tagSaved = true;
            }
        };

        private BroadcastReceiver mReadReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                if(/* SOME ERROR */) {
                    tagSaved = true;
                    return;
                }

                startService(/* START WRITING */);
            }
        };
    }

As I said, I think there's much room for improvement, but I could not end up with something better. My worry is that the BroadcastReceiver sends data back to the thread while it's sleeping so it might get lost. I didn't experience such an issue, but will it ever happen? Note that I cannot edit those two external services and I do not want tasks to be concurrent.

Can you propose a better way of doing this?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.