zoukankan      html  css  js  c++  java
  • 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);

        }

    }

  • 相关阅读:
    python_函数_文件
    Day_2_Python_str_list_dict的使用
    Day_1_Python_循环和格式化
    influxdb2.0版本部署+自启
    格式化Java内存工具JOL输出
    卷心菜的屯币日记
    influxDB时序数据库2.0FLUX查询语法使用记录
    两种转换2021-01-01T00:00:00Z为2021-01-01 00:00:00时间格式的方式(UTC时间转为yyyy-MM-dd HH:mm:ss)
    ThreadLocal的用处
    CentOS7使用ISO镜像文件作为离线Yum源
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429521.html
Copyright © 2011-2022 走看看