hexonxons
hexonxons

Reputation: 857

Android "application-global" thread

I have an application, which consists of 3 activities. In the first activity I start thread in onCreate() and stop it in onDestroy(), so I hope this thread will work while application running. In this thread I read data from database and send it to the server.

Main question: Is there right way to do this work, or first activity can be destroyed while application works? How to do that in the right way?

Side question: Due to multi-write to the database problem, is there a way to create class, which will manage SQLite writing and which will be accessible from every activity?

I think about using Application class, but have some doubts about that.

Upvotes: 0

Views: 688

Answers (4)

kdehairy
kdehairy

Reputation: 2730

  • for the first question, use IntentService to do the communication with the server.
  • for the second question, extend the SQLiteOpenHelper class and make it a Singleton. that's it.

Upvotes: 0

user1410657
user1410657

Reputation: 1294

The Application class can be used to manage application-global stuff. Whenever running a thread all the time is a good idea is another question.

  1. Implement an android.app.Application subclass. Should be in the root of your application package.

    package com.yourapp;        
    
    public class YourApplication extends Application {
        @Override
        public void onCreate() {
            // [...]
        }
        // [...]
    }
    
  2. Specify application class in the AndroidManifest.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.yourapp"
              android:versionName="1.0"
              android:versionCode="1.0">
        <application android:name="YourApplication"
                     android:label="@string/app_name"
                     android:icon="@drawable/icon">
        <!-- ... -->
        </application>
    </manifest>
    

Upvotes: 0

mah
mah

Reputation: 39847

You cannot rely on an activity not being destroyed when it is no longer needed, even if the rest of the application is active. You might be able to have all of your activities able to start/stop the thread though, and use an application global reference counter so you know when you want to start the thread again or just increase your reference count.

Regarding your database management class, it's ok to have multiple instances of your class; the SQLite database you're managing is thread-safe. If you have some other reason to have just a single instance of your helper, you can create it using the Singleton pattern such that the first client to access it creates the instance, and all subsequent clients get its pointer.

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75635

Sending data to server is usually not continous operation but repeatable job-type op, so it looks you shall rather use IntentService instead of Thread. Also setting in onCreate and destroying in onDestroy is not a best approach as your thread will be running even if application is in background (which usually means it will be of no use). Extending Application is quite ok, but I think you do not need to go that far.

Upvotes: 1

Related Questions