zoukankan      html  css  js  c++  java
  • 安卓 service

    public class MyService extends Service {
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            //throw new UnsupportedOperationException("Not yet implemented");
            return new Binder();  //这里必须要返回一个Binder实例
        }
        
        // 服务启动
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
        }
    
        // 服务销毁
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
        // 服务开始的时候 执行的方法
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            System.out.print("服务正在执行.....");
            new Thread() {
                @Override
                public void run() {
                    super.run();
    
                    while (true) {
                        System.out.print("服务正在执行....");
                        try {
                            sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            return super.onStartCommand(intent, flags, startId);
        }
    }
    public class MainActivity extends AppCompatActivity implements ServiceConnection {
    
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);  // 创建视图
            //setContentView(R.layout.my_layout);
    
            this.setContentView(R.layout.my_layout);
    
            // 启动服务
            findViewById(R.id.btnStartServcie).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    System.out.print("服务开始执行......");
                    Intent i = new Intent(MainActivity.this, MyService.class);
                    startService(i);
                }
            });
            // 停止服务
            this.findViewById(R.id.btnStopServcie).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity.this, MyService.class);
                    stopService(i);
                }
            });
    
    
            //btnBindServcie 绑定service
            this.findViewById(R.id.btnBindServcie).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity.this, MyService.class);
                    // 这里的MainActivity必须要实现ServiceConnection 重写onServiceConnected 和 onServiceDisconnected 方法
                    bindService(i, MainActivity.this, Context.BIND_AUTO_CREATE);
                }
            });
    
            //btnBindServcie 解除绑定service
            this.findViewById(R.id.btnUnBindServcie).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity.this, MyService.class);
                    unbindService(MainActivity.this);
                }
            });
    
            System.out.println("onCreate");
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            System.out.println("onStart");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            System.out.println("onResume");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            System.out.println("onPause");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            System.out.println("onStop");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            System.out.println("onDestroy");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            System.out.println("onRestart");
        }
    
    
        // 服务被绑定成功后被执行
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.print("service connected");
        }
    
    
        // 服务被杀掉后执行
        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.print("service DisConnected");
        }
    }

    这块不好理解,这个只能在以后的项目里面好好理解了。

  • 相关阅读:
    【转+补充】在OpenCV for Android 2.4.5中使用SURF(nonfree module)
    Delphi StarOffice Framework Beta 1.0 发布
    Angular ngIf相关问题
    angularjs文档下载
    公众号微信支付开发
    公众号第三方平台开发 教程六 代公众号使用JS SDK说明
    公众号第三方平台开发 教程五 代公众号处理消息和事件
    公众号第三方平台开发 教程四 代公众号发起网页授权说明
    公众号第三方平台开发 教程三 微信公众号授权第三方平台
    公众号第三方平台开发 教程二 component_verify_ticket和accessToken的获取
  • 原文地址:https://www.cnblogs.com/shaoshao/p/5860372.html
Copyright © 2011-2022 走看看