Android Open Source - AndroidText _1_ Service2






From Project

Back to project page AndroidText.

License

The source code is released under:

Apache License

If you think the Android project AndroidText 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.jiechic.androidtext._1_daemon;
//from  w  ww . j a  v a  2 s.co m
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/**
 * Created by jiechic on 14-10-15.
 */
public class _1_Service2 extends Service{
    private checkThread mThread;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

    private void startDaemon() {
        if (mThread == null || !mThread.isAlive()) {
            mThread = new checkThread();
            mThread.start();
        }
    }

    @Override
    public void onDestroy() {
        mThread.stopRun();
        mThread=null;
        System.gc();
        super.onDestroy();
    }

    class checkThread extends Thread{
        private Boolean isRun=true;
        @Override
        public void run() {
            super.run();
            while (isRun){
                if (!Util.isServiceAlive(_1_Service2.this,
                        _1_Service1.class)) {
                    startService(new Intent(_1_Service2.this,_1_Service1.class));
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void stopRun(){
            isRun=false;
        }
    }
}




Java Source Code List

com.jiechic.androidtext.ApplicationTest.java
com.jiechic.androidtext.MainActivity.java
com.jiechic.androidtext._1_daemon.Util.java
com.jiechic.androidtext._1_daemon._1_DaemonActivity.java
com.jiechic.androidtext._1_daemon._1_Service1.java
com.jiechic.androidtext._1_daemon._1_Service2.java
com.jiechic.androidtext.coreActivity.java