zoukankan      html  css  js  c++  java
  • Android---------------Service的学习

    一、创建与启动Servcie的三个步骤 :

      1. 创建一个类并继承Servcie

      2.在配置文件中注册服务

      3.通过Intent来启动服务  

      

    二、Service的两种启动方式

      1.startServce的启动方式 : 它是通过startService的方式来启动,不会与启动源有关系。当启动源销毁后,它的Service不会被销毁

       intent = new Intent();
       intent.setClass(MainActivity.this, MyService.class);
       startService(intent);
     

      2.bind的启动方式  : 它是通过bindService的方式来启动的,会和启动源绑定有关系。当启动源销毁后,它的Service也会跟随绑定来消失

       public class MyService extends Service {


       public class MyBinder extends Binder{
        public MyService getService(){
        return MyService.this;
        }
      }

        @Override
       public IBinder onBind(Intent intent) {
        Log.i("info", "onBind: ");
        return new MyBinder();
      }

       intent1 = new Intent();
       intent1.setClass(MainActivity.this , MyService.class);
       bindService(intent1, coon, Service.BIND_AUTO_CREATE);

       /////最应该的注意事项,一定小心它的解绑的位置,应该放在Activity的销毁位置解除绑定,否则会报错

         @Override
        protected void onDestroy() {
          super.onDestroy();
          unbindService(coon);
        }

    三、AIDL的使用步骤

      1.对外提供的服务,继承Binder类之外还需要抽取接口(定义规范,面向接口编程)

      2.把接口名改成AIDL,系统会自动生成相应的标准文件(把AIDL的public的访问修饰符删除),此步骤会在gen文件下生成相应的接口

      3.让对外提供的服务类class MyService extend Stub

       

  • 相关阅读:
    打印乘法口诀
    sum() 求和用法
    Python 2 和 Python 3 有哪些主要区别
    列表 enumerat 解包, 针对索引和元素
    冒泡排序,纯数字列表排序 解包,加中间值
    python 字符串与列表的相互转换 数据类型转换
    赋值 深浅拷贝
    python去掉字符串中空格的方法
    #上节多线程内容回顾#多线程的使用场景 #多进程的基本使用
    #queue队列 #生产者消费者模型
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9871712.html
Copyright © 2011-2022 走看看