How to start a new Thread in a service?

前端 未结 4 1526
甜味超标
甜味超标 2020-12-12 20:08

I am developing an Android app and I am doing some heavy work (bringing data from an online web page and parsing it to store in database) in a service. Currently, it is taki

相关标签:
4条回答
  • 2020-12-12 20:48

    You may define your jobs in a runnable object, use a thread object for running it and start this thread in your service's onStartCommand() function. Here is my notes:

    In your service class:

    1. define your main loop in an Runnable object
    2. create Thread object with the runnable object as parameter

    In your service class's onStartCommand method():

    1. call thread object's start function()

    my code :

    private Runnable busyLoop = new Runnable() {
        public void run() {
            int count = 1;
            while(true) {
                count ++;
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {
                    ;
                }
                ConvertService.running.sendNotification("busyLoop" + count);                      
            }
        }
    };
    
    public int onStartCommand(Intent intent, int flags, int startId) {
        sendNotification("onStartCommand");
        if (! t.isAlive()) {
            t.start();
        }
        return START_STICKY;
    }
    
    0 讨论(0)
  • 2020-12-12 20:55

    You can use HandlerThread and post to it, here is an example to service that has one.

    public class NetworkService extends Service {
    
        private HandlerThread mHandlerThread;
        private Handler mHandler;
        private final IBinder mBinder = new MyLocalBinder();
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            mHandlerThread = new HandlerThread("LocalServiceThread");
            mHandlerThread.start();
    
            mHandler = new Handler(mHandlerThread.getLooper());
        }
    
        public void postRunnable(Runnable runnable) {
            mHandler.post(runnable);
        }
    
        public class MyLocalBinder extends Binder {
            public NetworkService getService() {
                return NetworkService.this;
            }
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    }
    
    0 讨论(0)
  • 2020-12-12 21:00

    Example of new thread creation taken from Android samples (android-8\SampleSyncAdapter\src\com\example\android\samplesync\client\NetworkUtilities.java):

    public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {
    
                }
            }
        };
        t.start();
        return t;
    }
    

    runnable is the Runnable that contains your Network operations.

    0 讨论(0)
  • 2020-12-12 21:10

    Android commandment: thou shall not interact with UI objects from your own threads

    Wrap your Toast Display into runOnUIThread(new Runnable() { });

    0 讨论(0)
提交回复
热议问题