Service是android非常重要的组件之一,它运行在后台,不与用户进行交互。
java.lang.Object
|-- android.content.Context
|-- android.content.ContextWrapper
|-- android.app.Service
与Activity一样,Service也实现了ComponentCallbacks接口。
在默认情况下,Service运行在应用程序进程的主线程之中,如果需要在Service中处理一些网络连接等耗时的操作,那么应该将这些任务放在单独的线程中处理,避免阻塞用户界面。启动后的Service具有较高的优先级,一般情况下,系统会保证Service的正常运行,只有当前台的Activity正常运行的资源被Service占用的情况下,系统才会停止Service;当系统重新获得资源后,会根据程序的设置来决定是否重新启动原来的Service。
与使用Activity组件类似,使用Service组件也需要以下两个步骤:
- 定义一个继承Service的子类
- 在AndroidManifest.xml中使用<service>标签配置该Service
Service中定义了如下的生命周期方法,供开发者使用:
- IBinder onBind(Intent intent):该方法是Service子类必须实现的方法,该方法返回一个IBinder对象,应用程序可通过该对象与Service组件通信
- void onCreate():当Service组件第一次被创建后立即回调该方法
- void onDestroy():当Service关闭之前回调该方法
- void onStartCommand(Intent intent,int flags,int startId):该方法的早期版本是onStart(Intent intent,int startId),每次客户端调用startService()方法启动Service时都会回调该方法
- boolean onUnbind(Intent intent):当该Service上绑定的所有客户端断开连接时将会调用该方法
当其它组件通过Context.startService()方法启动Service时,系统会创建一个Service对象,并顺序调用onCreate()方法和onStartCommand()方法,在调用Context.stopService()或stopSelf()之前,Service一直处于运行的状态。如果多次调用startService()方法,系统只会多次调用onStartCommand()方法,不会重复调用onCreate()方法。无论调用了多少次startService()方法,只需调用一次stopService()方法就可以停止该Service。Service对象在销毁之前,onDestroy()方法会被调用,因此与资源释放相关的工作应该在此方法中完成。
当程序通过startService()方法和stopService()方法启动、停止Service()时,Service与访问者之间基本上不存在太多的关联,因此Service和访问者之间也无法进行通信和数据交换。下面我们介绍另一种给启动、停止Service的方法:bindService()与unbindService()。
bindService(Intent serivce,ServiceConnection connection,int flags)方法中
- 第一个参数通过Intent指定要启动的Service
- 第二个参数用于监听访问者与Service之间的连接情况
- 第三个参数指定绑定时是否自动创建Service(如果Service还未创建)。该参数可指定为0(不自动创建)或BIND_AUTO_CREATE(自动创建)
可能大家会对第二个参数——ServiceConnection对象感到陌生,下面进行简单说明。
该ServiceConnection对象用于监听访问者与Service之间的连接情况,当访问者与Service之间连接成功时,将会回调ServiceConnection对象的onServiceConnected(ComponentName name,IBinder service)方法;当Service所在的进程异常终止或由于其它原因终止时,导致该Service与访问者之间断开连接时,将会回调ServiceConnection对象的onServiceDisconnected(ComponentName name)方法(当主动调用unbindService()方法断开与Service的连接时,不会调用该回调方法)。
注意到ServiceConnection对象的onServiceConnected(ComponentName name,IBinder service)方法中有一个IBinder对象,该对象即可实现与被绑定Service之间的通信。
前面提到,开发Service类时必须提供IBinder onBind(Intent intent)方法,在绑定本地Service的情况下,onBind(Intent intent)方法所返回的IBinder对象将会传给ServiceConnection对象里的onServiceConnected(ComponentName name,IBinder service)方法的service参数,以实现与Service进行通信。
实际上开发时常会采用继承Binder(IBinder的实现类)的方式实现自己的IBinder对象。见如下代码:
首先,我们定义一个Service类:
public class BindService extends Service
{
//用count代表Service的运行状态
private int count;
//标志Service是否被关闭
private boolean quit;
//定义onBind()方法所返回的对象
private MyBinder binder=new MyBinder();
public class MyBinder extends Binder
{
public int getCount()
{
//获取Service的运行状态:count
return count;
}
}
@Override
public IBinder onBind(Intent intent)
{
System.out.println("Service is Binded!");
return binder;
}
//Service被创建时回调该方法
@Override
public void onCreate()
{
super.onCreate();
System.out.println("Serivce is Created!");
//启动一条新线程,动态改变count值
new Thread()
{
@Override
public void run()
{
while(!quit)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
count++;
}
}
}.start();
}
//Service被关闭之前回调该方法
@Override
public void onDestroy()
{
super.onDestroy();
this.quit=true;
System.out.println("Service is Destroyed!");
}
//Service断开连接时回调该方法
@Override
public boolean onUnbind(Intent intent)
{
System.out.println("Service is Unbinder");
return true;
}
}
接下来我们定义一个Activity来绑定该Service,并在Activity中通过MyBinder对象访问Service的内部状态。
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定Service" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解除绑定" />
<Button
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取Service状态" />
</LinearLayout>
Activity类如下:
public class MainActivity extends Activity
{
private Button bind,unbind,status;
//保持所启动的Service的IBinder对象
BindService.MyBinder binder;
//定义一个ServiceConnection对象
private ServiceConnection conn=new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
System.out.println("Service connected!");
//获取Service的onBind()方法所返回的MyBinder对象
binder=(BindService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name)
{
System.out.println("Service disconnected!");
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取界面按钮
bind=(Button)findViewById(R.id.bind);
unbind=(Button)findViewById(R.id.unbind);
status=(Button)findViewById(R.id.status);
//创建启动Service的Intent
final Intent intent=new Intent();
intent.setClassName(this, "nku.jerry.crazyit.crazyit_22.BindService");
bind.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//绑定指定Service
bindService(intent,conn,BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//解除绑定Service
unbindService(conn);
}
});
status.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//获取Service状态信息
Toast.makeText(MainActivity.this,"Service的count值为:"+binder.getCount(),Toast.LENGTH_LONG).show();
}
});
}
}
注意:不要忘记在Manifest.xml中注册该Service组件。
程序运行效果截图: