zoukankan      html  css  js  c++  java
  • Android 四大组件之 Service(一)

          Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互。

    1、Service的继承关系:

    java.lang.Object → android.content.Context → android.content.ContextWrapper → android.app.Service

    2、Sevice定义:

         Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件。其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行。另外,一个组件能够绑定到一个service与之交互(IPC机制),例如,一个service可能会处理网络操作,播放音乐,操作文件I/O或者与内容提供者(content provider)交互,所有这些活动都是在后台进行。

    3、Service有两种状态," 启动的 " 和 " 绑定 ":

    [1]通过startService()启动的服务处于" 启动的 "状态,一旦启动,service就在后台运行,即使启动它的应用组件已经被销毁了。通常started状态的service执行的任务并且不返回任何结果给启动者。比如当下载或上传一个文件,当这项操作完成时,service应该停止它本身。

    [2]" 绑定 "状态:通过调用bindService()来启动,一个绑定的service提供一个允许组件与service交互的接口,可以发送请求、获取返回结果,还可以通过夸进程通信来交互(IPC)。绑定的service只有当应用组件绑定后才能运行,多个组件可以绑定一个service,当调用unbind()方法时,这个service就会被销毁了。

    注意:默认情况下,service与activity一样都存在与当前进程的主线程中,所以,一些阻塞UI的操作,比如耗时操作不能放在service里进行,比如另外开启一个线程来处理诸如网络请求的耗时操作。如果在service里进行一些耗CPU和耗时操作,可能会引发ANR警告,这时应用会弹出是强制关闭还是等待的对话框。所以,对service的理解就是和activity平级的,只不过是看不见的,在后台运行的一个组件,这也是为什么和activity同被说为Android的基本组件。启动后的Service具有较高的优先级,一般情况下,系统会保证Service的正常运行,只有当前台的Activity正常运行的资源被Service占用的情况下,系统才会停止Service;当系统重新获得资源后,会根据程序的设置来决定是否重新启动原来的Service。

    4、Service的生命周期:

            

     注意:bind service的不同之处在于当绑定的组件销毁后,对应的service也就被kill了。

    service生命周期也涉及一些回调方法,这些方法都不用调用父类方法,具体如下:

    public class ExampleService extends Service {
        int mStartMode;       // indicates how to behave if the service is killed
        IBinder mBinder;      // interface for clients that bind
        boolean mAllowRebind; // indicates whether onRebind should be used
    
        @Override
        public void onCreate() {
            // The service is being created
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // The service is starting, due to a call to startService()
            return mStartMode;
        }
        @Override
        public IBinder onBind(Intent intent) {
            // A client is binding to the service with bindService()
            return mBinder;
        }
        @Override
        public boolean onUnbind(Intent intent) {
            // All clients have unbound with unbindService()
            return mAllowRebind;
        }
        @Override
        public void onRebind(Intent intent) {
            // A client is binding to the service with bindService(),
            // after onUnbind() has already been called
        }
        @Override
        public void onDestroy() {
            // The service is no longer used and is being destroyed
        }
    }
    

    5、Service的一个子类:IntentService

          IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,因此,这里提供一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作。而在一般的继承Service里面如果要进行耗时操作就必须另开线程,但是使用IntentService就可以直接在里面进行耗时操作,因为默认实现了一个worker thread。

    实现实例:

    public class defaultIntentService extends IntentService {
    
      //必须有构造函数, 并且必须带有worker thread的name作为参数调用super IntentService(String)
      public defaultIntentService() {
          super("defaultIntentService");
      }
    
      //通过从默认worker thread调用此带有intent的方法启动service
      //当方法返回IntentService会适时停止service
    @Override protected void onHandleIntent(Intent intent) { // 通常我们会做一些比如下载文件等的工作. // 比如实例中,睡眠五秒. long endTime = System.currentTimeMillis() + 5*1000; while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } } } }

    6、停止Service

    [1]如果service是非绑定的,最终当任务完成时,为了节省系统资源,一定要停止service,可以通过stopSelf()来停止,也可以在其他组件中通过stopService()来停止;

    [2]绑定的service可以通过onUnBind()来停止service。

        

  • 相关阅读:
    C# BulkCopy System.Data.SqlClient 数据库批量添加行数句
    SQL server 数据库优化表
    Bootstrap简介,特点,用法
    Entity Fromwork浅谈
    ADO,net 实体数据模型增、删、改,浅谈
    程序如何适应所有的难产客户
    访问数据库优化
    C#中哈希表(HashTable)的用法详解
    C# winform无边框窗体移动
    函数柯里化之加法add应用---add(1,2) add(1)(2) add(1)(2)(3) add(1,2,3)(4)
  • 原文地址:https://www.cnblogs.com/xinaixia/p/4105332.html
Copyright © 2011-2022 走看看