Android Open Source - ServicesTutorial Background 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  w  w w.  j  a v  a2  s.co 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.MainActivity;
import com.yanlu.android.services.app.utils.ThreadUtils;

/**
 * User: captain_miao
 * Date: 14-5-12
 * Time: ????5:08
 */
public class BackgroundService extends Service {
    private static final String TAG = "BackgroundService";
    private ThreadGroup myThreads = new ThreadGroup("ServiceWorker");

    @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(myThreads, 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);
                    final Intent i = new Intent(MainActivity.BROADCAST_ACTION);
                    i.putExtra(MainActivity.SERVICE_COUNTER, ++counter);
                    i.setPackage("com.yanlu.android.services.app"); //only broadcast to the project

                    //sendBroadcast(i);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "handleMessage", Toast.LENGTH_LONG).show();
                        }
                    });
//                    Message msg = mainHandler.obtainMessage(9999);
//                    mainHandler.sendMessage(msg);
//                    Message.obtain(mainHandler, 9999).sendToTarget();
//                    mainHandler.sendMessage();
                    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");
        myThreads.interrupt();
        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