• Service(一)


    一、Service简单示例:

    Service类:

    public class DummyService extends Service {

        @Override

        public IBinder onBind(Intent arg0) {

           // TODO Auto-generated method stub

           return null;

        }

        @Override

        public void onCreate(){

           super.onCreate();

        }

        @Override

        public void onStart(Intent intent,int startId){

           super.onStart(intent,startId);

           Log.i("Service-start", "服务开始了");

           Toast.makeText(this, "i am server", Toast.LENGTH_LONG).show();

        }

        @Override

        public void onDestroy() {

           // TODO Auto-generated method stub

           

           super.onDestroy();

           Log.i("Service-stop", "服务被销毁了");

        }

        

    }

    Activity类:

    public class ServerDemoActivity extends Activity implements OnClickListener{

        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            Button btn1=(Button)findViewById(R.id.btn1);

            btn1.setText("this is my activity");

            btn1.setOnClickListener(this);

            Button btn2=(Button)findViewById(R.id.btn2);

            btn2.setOnClickListener(this);

        }

        public void onClick(View v) {

           switch(v.getId()){

           case R.id.btn1:{

               doStart();

               break;

           }

           case R.id.btn2:{

               doStop();

               break;

           }

           }

        }

        public void doStart(){

           Intent starServer=new Intent(this,DummyService.class);

           this.startService(starServer);

        }

        public void doStop(){

           Intent starServer=new Intent(this,DummyService.class);

           this.stopService(starServer);

        }

    }

  • 相关阅读:
    以puppeteer抓取微指数,puppeteer基本示例,docker部署headless
    kubernets基于容器日志的报警和服务自动恢复
    mysql truncate 的问题
    kubernets轻量 contain log 日志收集技巧
    用logstash 作数据的聚合统计
    编译安装 logstash-output-jdbc
    技术沙龙记录1
    javascript的几个知识点scoping,execution context, hoisting, IIFE
    TDD 测试驱动开发
    css及HTML知识点
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429521.html
Copyright © 2020-2023  润新知