Android Service元件(1)
android service 和其他服務一樣,並沒有實際執行的介面,它執行在android 後臺。一般通過service為應用程式提供服務(比如,從Internet下載檔案,控制音樂播放器等)。Service的生命週期要比activity簡單的多,它只有三個階段(建立服務、開始服務、銷燬服務)。下面通過具體事例講解android的service元件。
1.建立android工程
在Eclipse中建立android工程 android_service_one
2.建立Service
在android工程中,建立包com.example.service,並新增方法MyService。該方法繼承與Service。該類用來展示服務的三個生命週期。
package com.example.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; /* * */ public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } //第一次開啟時呼叫 public void onCreate() { Log.d("MyService", "onCreate"); super.onCreate(); } //停止時呼叫 public void onDestory() { Log.d("MySerVice", "onDestory"); super.onDestroy(); } //開始時呼叫 public void onStart(Intent intent,int startId) { Log.d("MyService", "onStart"); super.onStart(intent, startId); } }
3.呼叫服務
在android 活動元件中呼叫剛剛建立的服務,呼叫之前需要在AndroidManifest.xml中對該服務進行配置。通過<service></service>來配置服務元件。
1 <service android:enabled="true" android:name="com.example.service.MyService"></service>
服務配置完成以後,可以呼叫該服務。服務與activity之間的互動依然通過Intent來進行通訊。啟動服務通過startservice(intent),停止服務通過stopservice(Intent intent)來完成。
Intent的宣告:Intent Intent=new Intent(this,MyService.class);
1 public void onClick(View v) {
2 // TODO Auto-generated method stub
3 switch(v.getId())
4 {
5 case R.id.button1:
6 startService(serviceintent);
7 break;
8 case R.id.button2:
9 stopService(serviceintent);
10 break;
11 }
12 }
4.Service與Activity繫結
Service與Activity繫結,當Activity啟動時,服務自動啟動。當Activity被銷燬時,服務也被銷燬。這裡需要在MyService中重寫 onBind、onRebind和onUnbind等幾個方法。
1 package com.example.service;
2
3 import android.app.Service;
4 import android.content.Intent;
5 import android.os.Binder;
6 import android.os.IBinder;
7 import android.util.Log;
8
9 /*
10 *
11 */
12 public class MyService extends Service {
13
14 private MyBinder myBinder= new MyBinder();
15
16 @Override
17 //成功繫結時呼叫該方法
18 public IBinder onBind(Intent intent) {
19 // TODO Auto-generated method stub
20 Log.d("MyService", "onBind");
21 return myBinder;
22 }
23
24 //重新繫結時呼叫該方法
25 public void onRebind(Intent intent)
26 {
27 Log.d("MyService", "onRebind");
28 super.onRebind(intent);
29 }
30
31 //解除繫結時呼叫該方法
32 public void onUnBind(Intent intent)
33 {
34 Log.d("MyService", "onUnbind");
35 super.onUnbind(intent);
36 }
37 //第一次開啟時呼叫
38 public void onCreate()
39 {
40 Log.d("MyService", "onCreate");
41 super.onCreate();
42 }
43 //停止時呼叫
44 public void onDestory()
45 {
46 Log.d("MySerVice", "onDestory");
47 super.onDestroy();
48 }
49 //開始時呼叫
50 public void onStart(Intent intent,int startId)
51 {
52 Log.d("MyService", "onStart");
53 super.onStart(intent, startId);
54 }
55
56 //繫結類
57 public class MyBinder extends Binder
58 {
59 //返回服務例項
60 public MyService getService()
61 {
62 return MyService.this;
63 }
64 }
65 }
5.呼叫服務
在Activity中宣告服務變數 Myservice myService,和宣告ServiceConnection連線變數。繫結變數呼叫bindService(serviceintent, conn, Context.BIND_AUTO_CREATE);
解除繫結呼叫,unbindService(ServiceConnection).
1 Intent serviceintent;
2 private MyService myService;
3 private ServiceConnection conn=new ServiceConnection() {
4
5 @Override
6 public void onServiceDisconnected(ComponentName name) {
7 // TODO Auto-generated method stub
8 myService=null;
9 Toast.makeText(MainActivity.this, "服務連線失敗", Toast.LENGTH_LONG).show();
10 }
11
12 @Override
13 public void onServiceConnected(ComponentName name, IBinder service) {
14 // TODO Auto-generated method stub
15 myService=((MyService.MyBinder)service).getService();
16 Toast.makeText(MainActivity.this, "服務連線成功", Toast.LENGTH_LONG).show();
17 }
18 };