2012年8月21日15:04:17
最近在研究一个播放器的源码,但是发现他是配合一个Service实现的功能。关于Service的内容之前没有碰过,所以这篇文章里摘抄的东西较多等自己熟悉了再逐步完善~
Service是Android组件中与Activity最相似的组件,都代表可执行的程序。
Service与Activity的区别在于:
Service一直在后台运行,没有用户界面。它和Activity一样一旦启动具有自己的生命周期。
一 简介
开发Service的步骤,
(1) 定义一个继承自Service的子类;
(2) AndroidManifest.xml文件中配置该Service;
系列生命周期的方法:
abstract IBinder onBind(Intent intent);//返回一个IBinder对象,应用程序可以通过该对象与Service组件通信。
void onCreate() 该Service第一次被创建后立即回调该方法
void onDestroy() 该Service被关闭之前将会回调该方法
void onStartCommand(Intent intent, int flags, int startId); 每次客户端调用startService(Intent)方法启动该Service时都会回调该方法;
boolean onUnbind(Intent intent):当该Service上绑定的所有客户端都断开连接时将会回调该方法;
08-21 22:45
Service的启动要由Activity通过startService或者bindService来启动,Inten负责传递参数。
他们两者的区别就是使Service的周期改变:
由startService启动的Service必须要有stopService来结束Service,否则会造成Activity结束Service继续运行的情况;
由bindService启动的Service可以由unbindService来结束,也可以在Activity结束之后(onDestroy)自动结束。
实例:
main.xml
03 | android:orientation="vertical" android:layout_width="fill_parent" |
04 | android:layout_height="fill_parent"> |
06 | android:layout_height="wrap_content"android:id="@+id/btnStartMyService" |
07 | android:text="StartMyService"> |
09 | android:layout_height="wrap_content"android:id="@+id/btnStopMyService" |
10 | android:text="StopMyService"> |
12 | android:layout_height="wrap_content"android:id="@+id/btnBindMyService" |
13 | android:text="BindMyService"> |
15 | android:layout_height="wrap_content"android:id="@+id/btnUnbindMyService" |
16 | android:text="UnbindMyService"> |
18 | android:layout_height="wrap_content" android:id="@+id/btnExit" |
程序源码:
TestService.java
01 | package com.testService; |
03 | import android.app.Activity; |
04 | import android.app.Service; |
05 | import android.content.ComponentName; |
06 | import android.content.Intent; |
07 | import android.content.ServiceConnection; |
08 | import android.os.Bundle; |
09 | import android.os.IBinder; |
10 | import android.util.Log; |
11 | import android.view.View; |
12 | import android.widget.Button; |
14 | public class TestService extends Activity { |
15 | Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit; |
17 | public void onCreate(Bundle savedInstanceState) { |
18 | super.onCreate(savedInstanceState); |
19 | setContentView(R.layout.main); |
20 | btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService); |
21 | btnStartMyService.setOnClickListener(new ClickEvent()); |
23 | btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService); |
24 | btnStopMyService.setOnClickListener(new ClickEvent()); |
26 | btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService); |
27 | btnBindMyService.setOnClickListener(new ClickEvent()); |
29 | btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService); |
30 | btnUnbindMyService.setOnClickListener(new ClickEvent()); |
32 | btnExit=(Button)this.findViewById(R.id.btnExit); |
33 | btnExit.setOnClickListener(new ClickEvent()); |
36 | public void onDestroy() |
39 | Log.e("Activity","onDestroy"); |
42 | private ServiceConnection _connection = new ServiceConnection() { |
44 | public void onServiceConnected(ComponentName arg0, IBinder arg1) { |
45 | // TODO Auto-generated method stub |
49 | public void onServiceDisconnected(ComponentName name) { |
50 | // TODO Auto-generated method stub |
53 | class ClickEvent implements View.OnClickListener{ |
56 | public void onClick(View v) { |
57 | Intent intent=new Intent(testService.this,MyService.class); |
58 | if(v==btnStartMyService){ |
59 | testService.this.startService(intent); |
61 | else if(v==btnStopMyService){ |
62 | testService.this.stopService(intent); |
64 | else if(v==btnBindMyService){ |
65 | testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE); |
67 | else if(v==btnUnbindMyService){ |
68 | if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑 |
69 | testService.this.unbindService(_connection); |
73 | testService.this.finish(); |
MyService.java
01 | package com.testService; |
03 | import android.app.Service; |
04 | import android.content.Intent; |
05 | import android.os.IBinder; |
06 | import android.util.Log; |
08 | public class MyService extends Service { |
09 | static public String ServiceState=""; |
11 | public IBinder onBind(Intent arg0) { |
12 | Log.e("Service", "onBind"); |
13 | ServiceState="onBind"; |
17 | public boolean onUnbind(Intent intent){ |
18 | super.onUnbind(intent); |
19 | Log.e("Service", "onUnbind"); |
20 | ServiceState="onUnbind"; |
25 | public void onCreate(){ |
27 | Log.e("Service", "onCreate"); |
28 | ServiceState="onCreate"; |
31 | public void onDestroy(){ |
33 | Log.e("Service", "onDestroy"); |
34 | ServiceState="onDestroy"; |
37 | public void onStart(Intent intent,int startid){ |
38 | super.onStart(intent, startid); |
39 | Log.e("Service", "onStart"); |
40 | ServiceState="onStart"; |
例子不太对头~等有空再更新掉重写吧。
阅读(308) | 评论(1) | 转发(0) |