实现与Service通信有两种方法,第一种使用bindService()来启动Service;
第二种就是使用startService()启动Service,但是使用BroadCast广播作为信使,
public class BindService extends Service
{
private int count;
private boolean quit;
// 定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
// 通过继承Binder来实现IBinder类
public class MyBinder extends Binder
{
public int getCount()
{
// 获取Service的运行状态:count
return count;
}
}
// 必须实现的方法
@Override
public IBinder onBind(Intent intent)
{
System.out.println("Service is Binded");
// 返回IBinder对象
return binder;
}
// Service被创建时回调该方法。
@Override
public void onCreate()
{
super.onCreate();
System.out.println("Service is Create