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);

        }

    }

  • 相关阅读:
    ubuntu 启用apache运行状态信息查看
    Loadrunner生成随机字符
    Linux 如何在 vi 里搜索关键字
    Loadrunnber 报错误:Error memory violation : Exception ACCESS_VIOLATION received.的一种情况
    Could not load file or assembly 'XXX' or one of its dependencies.
    C# 显示年月日星期
    C#中timer类的用法
    详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
    C# winform DataGridView 的18种常见属性
    WdatePicker日历控件使用方法
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429521.html
Copyright © 2011-2022 走看看