Android Open Source - AndroidText _1_ Daemon Activity






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  ww w.  j a v a2 s  .  c om
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import com.jiechic.androidtext.R;
import com.jiechic.androidtext.coreActivity;

/**
 * Created by jiechic on 14-10-15.
 */
public class _1_DaemonActivity extends coreActivity implements View.OnClickListener {
    private checkThread checkthread=null;

    private TextView _1_service1_status;
    private TextView _1_service2_status;

    private Intent service1;
    private Intent service2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1_daemon);

        initIntent();
        initView();

        if (null==checkthread){
            checkthread=new checkThread();
            checkthread.start();
        }

    }

    private void initIntent() {
        if (null==service1){
            service1=new Intent(_1_DaemonActivity.this,_1_Service1.class);
        }
        if (null==service2){
            service2=new Intent(_1_DaemonActivity.this,_1_Service2.class);
        }
    }

    private void initView() {
        _1_service1_status = (TextView) findViewById(R.id._1_service1_status);
        _1_service2_status = (TextView) findViewById(R.id._1_service2_status);
        findViewById(R.id._1_startservice).setOnClickListener(this);
        findViewById(R.id._1_stopservice1).setOnClickListener(this);
        findViewById(R.id._1_stopservice2).setOnClickListener(this);
        findViewById(R.id._1_stopservice).setOnClickListener(this);



    }

    @Override
    protected void onDestroy() {
        if (null!=checkthread){
            checkthread.stopRun();
            checkthread=null;
        }
        super.onDestroy();
    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    if (null!=_1_service1_status){
                        _1_service1_status.setText(getString(R.string._1_service1_status_running));
                    }
                    break;
                case 1:
                    if (null!=_1_service1_status){
                        _1_service1_status.setText(getString(R.string._1_service1_status_stop));
                    }
                    break;
                case 2:
                    if (null!=_1_service2_status){
                        _1_service2_status.setText(getString(R.string._1_service2_status_running));
                    }
                    break;
                case 3:
                    if (null!=_1_service2_status){
                        _1_service2_status.setText(getString(R.string._1_service2_status_stop));
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id._1_startservice:
                if (null!=service1){
                    startService(service1);
                }
                if (null!=service2){
                    startService(service2);
                }
                break;
            case R.id._1_stopservice1:
                if (null!=service1){
                    stopService(service1);
                }
                break;
            case R.id._1_stopservice2:
                if (null!=service2){
                    stopService(service2);
                }
                break;
            case R.id._1_stopservice:
                if (null!=service1){
                    stopService(service1);
                }
                if (null!=service2){
                    stopService(service2);
                }
                break;
        }
    }


    class checkThread extends Thread{
        private Boolean isRun=true;
        @Override
        public void run() {
            super.run();
            while (isRun){
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (Util.isServiceAlive(_1_DaemonActivity.this,_1_Service1.class)){
                    //Service1 is Running
                    handler.sendEmptyMessage(0);
                }else{
                    //Service1 is Stop
                    handler.sendEmptyMessage(1);
                }
                if (Util.isServiceAlive(_1_DaemonActivity.this,_1_Service2.class)){
                    //Service2 is Running
                    handler.sendEmptyMessage(2);
                }else{
                    //Service1 is Stop
                    handler.sendEmptyMessage(3);
                }
            }
        }

        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