zoukankan      html  css  js  c++  java
  • startService和binderService的混合使用时的生命周期

    创建MyService类

    创建一个MyService类继承与Service:

    public class MyService extends Service {
        @Override
        public void onCreate() {
            Log.i(getClass().getSimpleName(),"onCreate");
            super.onCreate();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i(getClass().getSimpleName(),"onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            Log.i(getClass().getSimpleName(),"onDestory");
            super.onDestroy();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            Log.i(getClass().getSimpleName(),"onBind");
            return new MyBinder();
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.i(getClass().getSimpleName(),"onUnbind");
            return super.onUnbind(intent);
        }
    
        public class MyBinder extends Binder{
            MyService getService(){
                return MyService.this;
            }
        }
    }
    

    绑定和开启MyService

    startService和binderService:

    public class MainActivity extends Activity implements View.OnClickListener {
    
        private ServiceConnection conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myService = ((MyService.MyBinder)service).getService();
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        };
        private MyService myService;
        private Intent intent;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.btn1).setOnClickListener(this);
            findViewById(R.id.btn2).setOnClickListener(this);
            findViewById(R.id.btn3).setOnClickListener(this);
            findViewById(R.id.btn4).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            intent = new Intent(MainActivity.this,MyService.class);
            switch (view.getId()) {
                case R.id.btn1:
                    startService(intent);
                    break;
                case R.id.btn2:
                    bindService(intent,conn,BIND_AUTO_CREATE);
                    break;
                case R.id.btn3:
                    stopService(intent);
                    break;
                case R.id.btn4:
                    unbindService(conn);
                    break;
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            stopService(intent);
            unbindService(conn);
        }
    }
    

    1. startService ===>bindService ==>stopService ==>unbindService;

      stopService并没有调用Service中的任何方法,因为bindService还没有与MyService取消绑定
    2. bindService ==>startService ==>stopService ==>unbindService

      stop依旧没有调用Service中的方法;
    3. startService ==>bindService ==>unbindService ==>stopService

      到这一步我们可以知道,最后与Service解除绑定/停止服务的那个方法,才会执行onDestory,
    GitHub:https://github.com/godfunc
    博客园:http://www.cnblogs.com/godfunc
    Copyright ©2019 Godfunc
  • 相关阅读:
    127. Word Ladder(单词变换 广度优先)
    150. Evaluate Reverse Polish Notation(逆波兰表达式)
    32. Longest Valid Parentheses(最长括号匹配,hard)
    20. Valid Parentheses(括号匹配,用桟)
    递归桟相关
    python编写计算器
    python打印9宫格,25宫格等奇数格,且横竖斜相加和相等
    基于百度人工智能图片识别接口开发的自动录题系统
    自动收集有效IP代理
    python数据储存
  • 原文地址:https://www.cnblogs.com/Godfunc/p/6553831.html
Copyright © 2011-2022 走看看