• 安卓 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");
        }
    }

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

  • 相关阅读:
    欧拉计划之题目2:在斐波那契数列中,找出4百万以下的项中值为偶数的项之和。
    MFC非模态对话框的销毁
    MFC:只允许产生一个应用程序实例的具体实现
    从_tiddata看CRT的线程不安全函数
    关于消息循环的深入分析
    MFC:关于MFC窗口对象(CWnd对象)与Window对象(HWND所指对象)的销毁问题
    使用FindFirstFile和FindNextFile对给定目录下所有文件进行广度优先遍历
    工作线程的消息循环与通信
    MFC和设计模式
    _endthreadex与CloseHandle
  • 原文地址:https://www.cnblogs.com/shaoshao/p/5860372.html
Copyright © 2020-2023  润新知