美文网首页
Android服务(Service)

Android服务(Service)

作者: whamu22 | 来源:发表于2017-07-07 10:18 被阅读0次

    定义

    Service是Android四大组件之一,也是可执行的程序,有自己的生命周期。创建、配置Service和创建、配置Activity的过程相似。和Activity一样,都是从Context派生出来的。

    分类

    按启动方式分

    采用start的方式开启服务

    1. 定义一个类继承Service
    2. 在Manifest.xml文件中配置该Service
    3. 使用Context的startService(Intent)方法启动该Service
    4. 不再使用时,调用stopService(Intent)方法停止该服务

    生命周期
    onCreate()--->onStartCommand()(onStart()方法已过时) ---> onDestory()
    说明
      如果服务已经开启,不会重复的执行onCreate(),而是会调用onStart()和onStartCommand()。服务停止的时候调用onDestory()。服务只会被停止一次。
    特点
    一旦服务开启跟调用者(开启者)就没有任何关系了。开启者退出了,开启者挂了,服务还在后台长期的运行。开启者不能调用服务里面的方法。

    示例

    public class LocalService extends Service {
        // Binder given to clients
        private final IBinder mBinder = new LocalBinder();
        // Random number generator
        private final Random mGenerator = new Random();
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
        public class LocalBinder extends Binder {
            public LocalService getService() {
                return LocalService.this;
            }
        }
    
        /**
         * method for clients
         */
        public int getRandomNumber() {
            return mGenerator.nextInt(100);
        }
    }
    
    // 调用
    Intent intent = new Intent(this, LocalService.class);
    startService(intent);
    // 停止
    stopService(intent);
    

    采用bind的方式开启服务

    步骤

    1.定义一个类继承Service
    2.在Manifest.xml文件中配置该Service
    3.使用Context的bindService(Intent, ServiceConnection, int)方法启动该Service
    4.不再使用时,调用unbindService(ServiceConnection)方法停止该服务

    生命周期
    onCreate() --->onBind()--->onunbind()--->onDestory()

    注意
      绑定服务不会调用onstart()或者onstartcommand()方法

    特点
      bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。绑定者可以调用服务里面的方法。

    示例

    public class LocalService extends Service {
        // Binder given to clients
        private final IBinder mBinder = new LocalBinder();
        // Random number generator
        private final Random mGenerator = new Random();
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
        /**
         * Class used for the client Binder.  Because we know this service always
         * runs in the same process as its clients, we don't need to deal with IPC.
         */
        public class LocalBinder extends Binder {
            public LocalService getService() {
                // Return this instance of LocalService so clients can call public methods
                return LocalService.this;
            }
        }
    
        /**
         * method for clients
         */
        public int getRandomNumber() {
            return mGenerator.nextInt(100);
        }
    }
    
    // 绑定
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    
    // 获取服务
    private LocalService mService;
    private boolean mBound = false;
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            LocalService.LocalBinder binder = (LocalService.LocalBinder) iBinder;
            mService = binder.getService();
            mBound = true;
        }
    
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mBound = false;
        }
    };
    // 调用服务的方法
    if (mBound) {
        int number = mService.getRandomNumber();
        // do somtheing
    }
    
    // 调用者关闭时解绑服务
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
    

    可用作多线程的服务(IntentService)

    相关文章

      网友评论

          本文标题:Android服务(Service)

          本文链接:https://www.haomeiwen.com/subject/sudlhxtx.html