学习Service,按照自己的思路,也是和Activity一样先掌握Service生命周期的基础之上,再去掌握Service的使用。
Service的生命周期表
Service的使用方式
一般来说使用Service都是自己自定义的,下面给出我自己自定义的Service,读者也可以自己根据需要进行封装,以便以后方便自己开发使用。
/**
* Copyright<2014> yummylau Inc.
*/
package com.example.yummyserviceconnectactivity;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
/**
* 重写Service类,实现binder内部类进行外部通信
* 一般来说Binder作为Service的内部类,这对于绑定本地的Service并作为外部通信的一种手段
* @author yummy
* @date 2014-07-15
*/
public class MyService extends Service{
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder{
//根据自己的需要对Service内部数据进行操作
}
/**
* 绑定Service是会回调这个方法,返回Binder对象
* @param arg0
* @return IBinder
*/
@Override
public IBinder onBind(Intent arg0) {
return binder;
}
//创建的时候会回调这个方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
/**
* Service创建的时候会回调这个方法
*/
@Override
public void onCreate() {
super.onCreate();
//根据需要可以创建异步后台线程或者其他的操作
}
/**
* Service关闭的时候会回调这个方法
*/
@Override
public void onDestroy() {
super.onDestroy();
//进行后尾处理
}
/**
* Service被断开连接的时候会回调这个方法
* @param intent
* @return boolean
*/
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
//断开后进行操作
}
}
/**
* Copyright<2014> yummylau Inc.
*/
package com.example.yummyserviceconnectactivity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
/**
* 本地Activity测试类,该类利用绑定Service实现之交流
* 除了重写基本的方法会,也在AndroidManifest进行配置,说明该Service被哪些Intent所启动。
* 事实上,有两种启动Service的方法,一种是startService(),启动的Service与启动者没有关联;另一种就是用绑定式来启动,如下模板所说。
* 前者被自己停止或者被调用者关掉就执行onDestory()而后者一旦取消绑定就会执行onDestory()
* 思路:前提有service存在
* 执行bindService(),如果连接service成功会回调conn的onServiceConnected()方法,于是能通过接受参数拿到Binder对象,这个
* Binder对象正是从外面YummyService内部实现的Binder对象。
* @author yummy
* @date 2014-07-15
*/
public class ServiceActivity extends Activity {
//保存所启动的Service的IBinder对象
MyService.MyBinder binder;
//定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
/**
* 当ServiceActivity与Service断开连接的时候会回调这个方法
*/
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
/**
* 当ServiceActivity与Service连接成功的时候会回调这个方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
binder = (MyService.MyBinder)service;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建启动Service的Intent
final Intent intent = new Intent();
intent.setAction("com.example.service.YUMMY_SERVICE");
//绑定service
//bindService(Intent intent,ServiceConnection conn,Service.BIND_AUTO_CREATE);
//解除service
//unbindService(ServiceConnection conn);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
现在来解释下两种启动的区别(上述ServiceActivity属于绑定启动)
自主开启
被开启的service通过其他组件调用 startService()被创建。这种service可以无限地运行下去,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。
当service被停止时,系统会销毁它。
绑定开启
被绑定的service是当其他组件(一个客户)调用bindService()来创建的。客户可以通过一个IBinder接口和service进行通信。
客户可以通过 unbindService()方法来关闭这种连接。
一个service可以同时和多个客户绑定,当多个客户都解除绑定之后,系统会销毁service。
这两条路径并不是完全分开的。
即是说,你可以和一个已经调用了 startService()而被开启的service进行绑定。
比如,一个后台音乐service可能因调用 startService()方法而被开启了,稍后,可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过bindService()将一个activity和service绑定。这种情况下,stopService()或 stopSelf()实际上并不能停止这个service,除非所有的客户都解除绑定。
Service生命周期的管理
当绑定service和所有客户端解除绑定之后,Android系统将会销毁它,(除非它同时被onStartCommand()方法开启)。因此,如果你的service是一个纯粹的绑定service,那么你不需要管理它的生命周期。
然而,如果你选择实现onStartCommand()回调方法,那么你必须显式地停止service,因为service此时被看做是开启的。这种情况下,service会一直运行直到它自己调用 stopSelf()或另一个组件调用stopService(),不论它是否和客户端绑定。
然而,如果你选择实现onStartCommand()回调方法,那么你必须显式地停止service,因为service此时被看做是开启的。这种情况下,service会一直运行直到它自己调用 stopSelf()或另一个组件调用stopService(),不论它是否和客户端绑定。
另外,如果你的service被开启并且接受绑定,那么当系统调用你的 onUnbind()方法时,如果你想要在下次客户端绑定的时候接受一个onRebind()的调用(而不是调用 onBind()),你可以选择在 onUnbind()中返回true。onRebind()的返回值为void,但是客户端仍然在它的 onServiceConnected()回调方法中得到 IBinder 对象。
(展示了这种service(被开启,还允许绑定)的生命周期)
以上就是本人对Service一些基本需要掌握的知识的小结。后序可能会对Service的更深层次的知识做解释,希望对大家有帮助。