Android四大组件之Service

来源:互联网 发布:php try catch 编辑:程序博客网 时间:2024/06/16 19:28

在Android四大组件中,Service和Activity两个比较相似,区别是Activity用于前台,Service用于后台。

当你的程序不需要用组件呈现界面时,这时候用Service。典型范例就是播放音乐,界面退出后仍需播放音乐,这时需要的就是Service后台运行了。


Service创建

1、定义一个继承Service的子类
2、在AndroidMainifest.xml文件中配置该Service。
   这个过程是不是跟创建Activity有点相似呢。是吧。

Service启动和停止

1、调用Context里面定义的startService()、stopService()两个方法即可。
   但是,这种方式启动的Service和访问者之间无法进行通信和数据交换。
2、使用bindService(Intent service,ServiceConnection conn,int flags)和unbindService()启动和关闭,可以实现通信和数据交换。

   实现通信和数据交换的原理是什么呢?
   先来看bindService(Intent service,ServiceConnection conn,int flags)三个参数的含义:
   > service:毫无疑问这是准备启动的service
   > conn   :这是一个ServiceConnection对象,他的作用就是判断连接的情况,
             当连接成功的时候会回调ServiceConnection对象的onServiceConnected()方法,
             当连接出现问题,或产生异常情况,而这种情况不是调用者主动断开连接时,会回调ServiceConnection的onServiceDisconnected()方法;
   > flags  :在绑定时是否自动创建Service(在还未创建的情况下),可以设置为
             0 - 不自动创建;
             BIND_AUTO_CREATE - 自动创建;                                                 
  

   连接成功的关键就在第二个参数,这个参数对象的onServiceConnected方法中有一个IBinder对象,IBinder对象就像通信的使者一样,用来传递数据。
   
   下面附上Android疯狂讲义上面的例子源码:
   
   Service子类中onBind()是必须实现的方法,这个方法返回一个IBinder对象,向调用者通信。而返回的这个IBinder对象通常用继承Binder实现自己的IBinder对象。
public class BindService extends Service{private int count;private boolean quit;// 定义onBinder方法所返回的对象private MyBinder binder = new MyBinder();<span style="background-color: rgb(102, 255, 153);">// 通过继承Binder来实现IBinder类<span style="color:#333333;">public class MyBinder extends Binder //①{public int getCount(){// 获取Service的运行状态:countreturn count;}}// 必须实现的方法,绑定该Service时回调该方法@Overridepublic IBinder onBind(Intent intent){System.out.println("Service is Binded");// 返回IBinder对象return binder;}</span></span>// Service被创建时回调该方法。@Overridepublic void onCreate(){super.onCreate();System.out.println("Service is Created");// 启动一条线程、动态地修改count状态值new Thread(){@Overridepublic void run(){while (!quit){try{Thread.sleep(1000);}catch (InterruptedException e){}count++;}}}.start();}// Service被断开连接时回调该方法@Overridepublic boolean onUnbind(Intent intent){System.out.println("Service is Unbinded");return true;}// Service被关闭之前回调该方法。@Overridepublic void onDestroy(){super.onDestroy();this.quit = true;System.out.println("Service is Destroyed");}}


 下面是一个Activity来绑定该Service,在Activity中定义一个Service中的MyBinder类来访问Service内部状态。

public class BindServiceTest extends Activity{Button bind, unbind, getServiceStatus;// 保持所启动的Service的IBinder对象BindService.MyBinder binder;<span style="background-color: rgb(102, 255, 153);">// 定义一个ServiceConnection对象private ServiceConnection conn = new ServiceConnection(){// 当该Activity与Service连接成功时回调该方法@Overridepublic void onServiceConnected(ComponentName name, IBinder service){System.out.println("--Service Connected--");// 获取Service的onBind方法所返回的MyBinder对象binder = (BindService.MyBinder) service; //①}// 当该Activity与Service断开连接时回调该方法@Overridepublic void onServiceDisconnected(ComponentName name){System.out.println("--Service Disconnected--");}};</span>@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取程序界面中的start、stop、getServiceStatus按钮bind = (Button) findViewById(R.id.bind);unbind = (Button) findViewById(R.id.unbind);getServiceStatus = (Button) findViewById(R.id.getServiceStatus);// 创建启动Service的Intentfinal Intent intent = new Intent();// 为Intent设置Action属性intent.setAction("org.crazyit.service.BIND_SERVICE");bind.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View source){// 绑定指定SerivcebindService(intent, conn, Service.BIND_AUTO_CREATE);}});unbind.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View source){// 解除绑定SerivceunbindService(conn);}});getServiceStatus.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View source){// 获取、并显示Service的count值Toast.makeText(BindServiceTest.this,"Serivce的count值为:" + binder.getCount(),Toast.LENGTH_SHORT).show(); //②}});}}

Service的生命周期

两种不同的启动Service的方式有不同的生命周期

 非绑定式Service生命周期:
  onCreate()->onStartCommand()->onDestroy()
 绑定式Service生命周期:
  onCreate()->onBind()->onUnbind()->onDestroy()


0 0
原创粉丝点击