Android 的服务-Service

Service是Android四大组件之一,常用于后台运行任务。Service分为StartedService和BoundService,前者通过startService()启动,无法直接通信;后者通过bindService()启动,允许组件间通信。Service默认在主线程运行,需避免执行重量级操作。本文详细介绍了Service的启动方式、生命周期及通信机制。

Service基础:

Service与Activity同为四大组件之一, 与Activity不同的是Service是默默运行在后台的幕后英雄.开启一个Service之后你可以通过它在后台执行想要执行的任务, 比如网络连接, 播放音乐, 处理文件等操作.

Service默认开启在主线程中, 所以不能在Service中直接运行重量级操作.

Service有两种启动方式, 普通的StartedService,调用startService()方法启动, 启动之后不能再与启动它的组件进行通信. 还有加强版的BoundService, 调用bindService()方法启动, 启动之后可以保持与启动它的组件通信.

Service的子类必须实现其onBind()方法, 如果不希望Service被绑定, 那么该方法需要返回null.

StartedService:

通过startService()方法启动一个Service, 这种情况下Service不会返回结果给启动者. 可以在Service内部调用stopSelf()或者在Service外部调用stopService()来停止这个Service. 如果不调用这些方法, 那么Service将一直运行直到被系统回收, 即便启动它的Activity被销毁, 该Service也不会因此而被销毁.

启动一个StartedService三部曲:

1.      在Mainifest.xml文件中声明一个Service.

2.      继承Service类或者它的子类.

3.      在组件中调用startService()方法, 传入一个Intent参数.

在Service初次启动的时候, 会依次调用onCreate()方法和onStartCommand()方法. 如果调用startService()方法来启动一个正在运行中的Service, 那么会调用该Service的onStartCommand()方法, 而不会再调用onCreate().

BoundService:

其实BoundService应该算是StartedService的进阶, 因为我们可以通过调用bindService来启动一个新的Service, 也用它来绑定一个已经存在的Service. 当我们希望Activity或者其他组件需要与Service通信的时候, 我们应该用BoundService,

BoundService在实现上与StartService的不同之处:

1.      两者都需要实现onBind()方法, 但是返回值却不同, StartedService返回null, 而BoundService必须返回一个IBinder对象.

2.      通过bindService()启动的Service, 无需调用stopSelf()或者stopService()来结束, 当没有组件绑定该Service的时候, 它会被系统回收. 如果该Service是通过startService()启动的, 那么在该Service没有被绑定的时候, 依然需要调用stopSelf()或者stopService()来结束它.

BoundService与组件间的通信(同时也是IPC的部分重要内容, 另起文章):

前面提到了, BoundService和StartService区别就是是否可以在Service启动之后跟启动它的组件通信. 在了解通信之前, 先介绍一下bindService()的过程. APP组件(客户端)可以通过bindService()方法来绑定一个Service, 当被绑定时, Service会调用onBind()方法, 该方法返回一个IBinder对象给客户端. 绑定过程是异步的, 想要获取这个IBinder对象, 那么客户端需要创建一个ServiceConnection对象, 并在bindService()的时候作为参数传给它. ServiceConnection对象的回调方法onServiceConnected()中我们可以获取到这个IBinder对象. 另外在四大组件中, Broadcast receiver不能绑定Service. 绑定一个Service的步骤:

1.      创建一个ServiceConnection对象, 并且重写它的两个方法:

         onServiceConnected(): 在Service的onBind()后调用, 用于返回IBinder对象.

         onServiceDisconnected(): 在于Service失去连接之后调用, 比如Service挂了或者被系统回收了, 但是正常解除绑定的时候不会调用该方法.

2.      调用bindService()方法, 并将ServiceConnection对象作为参数传给它.

3.      系统调用onServiceConnected()方法.

4.      客户端调用unbindService()方法解除绑定.

那么BoundService是如何通信的呢? 主要通信方法有三种:

1.      当Service只是服务于APP内部, 并且与启动Service的组件之间没有跨进程的时候, 应该通过继承Binder类来实现. 实现方法是酱婶儿的:

        a.      继承Binder类. 该类需要提供可以供组件调用的公共方法; 或者也可以带Service的实例, 以便调用Service的公共方法; 再或者其它的什么类, 总之得带个公共方法, 要不                    然就没意义了.

        b.      在onBind()中返回该类的一个实例.

        c.      在启动Service的组件中, 通过onServiceConected()方法可以获取到传过来的Binder子类对象, 通过该对象, 就可以调用其提供的接口了.

一言以B之就是: Service提供接口给启动它的组件调用,中间通过Binder传递. 只要不是跨进程, 都可以使用这种方法来跟启动Service的组件通信. 官方栗子:

public class LocalService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    /**
     * 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 {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
    }
}

该栗子中Binder的子类是LocalBinder,它提供的接口是getService()以提供一个Service实例, 这样在启动Service的组件中就可以调用Service的公共接口了. 这里是使用该Service的例子:

public class BindingActivity extends Activity {
    LocalService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    /** Called when a button is clicked (the button in the layout file attaches to
      * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

2.      当该Service需要跨APP或者跨进程提供服务的时候, 上述方法就不管用了. 需要使用Messenger. 以这种方式, Service可以通过Handler来处理不同的Message对象. 这是跨进程通信中最简单的方式, 因为Messenger的消息队列是在同一个线程中处理的, 这样我们就不需要考虑讨厌的线程安全的问题了. 使用Messenger通信的步骤:

a.      创建一个Handler的子类, 用于处理绑定Service的组件(客户端)发来的消息.

b.      使用该类初始化一个Messenger对象.

c.      Messenger创建一个IBinder, 并通过onBind()方法返回给客户端.

d.      客户端通过收到的IBinder可以实例化一个Messenger.

e.      通过该Messenger调用send方法就可以发送Message给Service了.

一言以B之: 就是通过Messenger创建了一个发Message的通道. 官方栗子:

public class MessengerService extends Service {
    /** Command to the service to display a message */
    static final int MSG_SAY_HELLO = 1;

    /**
     * Handler of incoming messages from clients.
     */
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SAY_HELLO:
                    Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    /**
     * Target we publish for clients to send messages to IncomingHandler.
     */
    final Messenger mMessenger = new Messenger(new IncomingHandler());

    /**
     * When binding to the service, we return an interface to our messenger
     * for sending messages to the service.
     */
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
        return mMessenger.getBinder();
    }
}

看上去跟Handler-Thread的通信方法差不多. 下面是客户端Activity的栗子:

public class ActivityMessenger extends Activity {
    /** Messenger for communicating with the service. */
    Messenger mService = null;

    /** Flag indicating whether we have called bind on the service. */
    boolean mBound;

    /**
     * Class for interacting with the main interface of the service.
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the object we can use to
            // interact with the service.  We are communicating with the
            // service using a Messenger, so here we get a client-side
            // representation of that from the raw IBinder object.
            mService = new Messenger(service);
            mBound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;
            mBound = false;
        }
    };

    public void sayHello(View v) {
        if (!mBound) return;
        // Create and send a message to the service, using a supported 'what' value
        Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
        try {
            mService.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to the service
        bindService(new Intent(this, MessengerService.class), mConnection,
            Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
}

上边的栗子是单项通信, 只能客户端发送Message,Service处理, 双向的话原理一样, 可以通过客户端发送Messenger给Service.

3.      使用AIDL: 如果Service是多线程的, 应该考虑用AIDL. 这种方法相对复杂, 还要考虑线程安全的问题, 所以我打算以后再详细研究它. 另外Messenger也是基于AIDL的. 

Service生命周期:

跟Activity一样, Service也有自己的生命周期, 但是比Activity要简单些. 不同启动模式下Service的生命周期: 


onCreate()和onDestroy()方法应该用于初始化和释放资源, 在任何启动方式下, 这两种方法都会被回调.

在图中的ActiveLifetime中, 不同的启动方式下Service的表现有所不同, 当使用startService()方法启动时, Service调用onStartCommand(). 任何一种Service被bindService()的时候, 都会调用onBind().

IntentService:

前文有曰: Service默认情况下是跑在主线程的, 除了在Service中新建线程之外, Android还提供了更好用的方法, 就是使用IntentService, IntentService使用很简单, 只是在普通的Service里加了一个onHandleIntent()方法. IntentService的使用有以下规则:

1.      onStartCommand()方法依然运行在主线程里.

2.      onHandleIntent()方法运行在独立的线程里, 在onStartCommand()方法执行完毕之后, Android会调用onHandleIntent()方法, 并且会将intent作为参数传给它.

3.      onHandleIntent()执行完毕之后, Service会结束. 不用手动调用stopSelf().

4.      该类会提供默认的onBind()方法, 并且返回null.

5.      该类会提供默认的onStartCommand()方法, 并返回super.onStartCommand(intent, flags, startId);

6.      该类需要一个构造函数, 并调用父类的构造函数, 参数是工作线程的名字.

这里写一个简单的Demo来说明它的运行原理:

public class MyIntentService extends IntentService {

    private static final String TAG = "IntentService";

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate thread id: " + Thread.currentThread().getId());
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand thread id: " + Thread.currentThread().getId());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, "onHandleIntent thread id: " + Thread.currentThread().getId());
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy thread id: " + Thread.currentThread().getId());
        super.onDestroy();
    }
}

下面是MainActivity:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "IntentService";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "MainActivity thread id: " + Thread.currentThread().getId());
        Button btnStartService = (Button)findViewById(R.id.startService);
        btnStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MyIntentService.class);
                startService(intent);
            }
        });
    }
}
多次点击按钮之后, 结果如下:


刚好可以证明上面的规则.


参考: http://developer.android.com/guide/components/services.html 







评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值