Android Open Source - ServicesTutorial Handler Service






From Project

Back to project page ServicesTutorial.

License

The source code is released under:

Apache License

If you think the Android project ServicesTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yanlu.android.services.app.service;
/*from  ww  w .  j a  v a2 s .c o m*/
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.yanlu.android.services.app.utils.ThreadUtils;

/**
 * User: captain_miao
 * Date: 14-5-12
 * Time: ????5:08
 */
public class HandlerService extends Service {
    private static final String TAG = "HandlerService";
    @Override
    public void onCreate() {
        super.onCreate();
        ThreadUtils.logThreadSignature();
        Log.v(TAG, "in onCreate()");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        Log.v(TAG, "in onStartCommand() " + ", startId = " + startId);
        new Thread(new ServiceWorker(0), "BackgroundService").start();
        return START_STICKY;
    }
    Handler mainHandler = new Handler(Looper.getMainLooper());
    class ServiceWorker implements Runnable {
        private int counter = -1;

        public ServiceWorker(int counter) {
            this.counter = counter;
        }

        public void run() {
            final String TAG2 = "ServiceWorker:" +
                    Thread.currentThread().getId();
            // do background processing here...
            while (true) {
                try {
                    Log.v(TAG2, "sleeping for 10 seconds. counter = " + counter);

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "handleMessage" + ++counter, Toast.LENGTH_LONG).show();
                        }
                    });

                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    Log.v(TAG2, "... sleep interrupted");
                    break;
                }
            }
        }

    }


    @Override
    public void onDestroy() {
        Log.v(TAG, "in onDestroy(). Interrupting threads and cancelling notifications");
        stopSelf();
        super.onDestroy();

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;//local service don't use
    }
}




Java Source Code List

com.yanlu.android.services.app.MainActivity.java
com.yanlu.android.services.app.service.BackgroundService.java
com.yanlu.android.services.app.service.HandlerService.java
com.yanlu.android.services.app.service.MyIntentService.java
com.yanlu.android.services.app.service.StockQuoteService.java
com.yanlu.android.services.app.utils.DeferWorkHandler.java
com.yanlu.android.services.app.utils.ThreadUtils.java