Service是Androd系統提供的四種組件之一,它的地位和Activity是並列的,只不過沒有Activity的使用頻率高。顧名思義Service就是運行在後臺的一種服務程序,一般很少和用戶交互,因此沒有可視化界面。
1:我們通過佈局文件layout/main.xml創建一個啓動、停止、及綁定一個Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/startButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啓動Service"
/>
<Button
android:id="@+id/stopButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止Service"
/>
<Button
android:id="@+id/bindButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綁定Service"
/>
<Button
android:id="@+id/unbindButton04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除綁定"
/>
</LinearLayout>
|
2:我們創建一個MainActivity.java來實現Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package com.android.test;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button startBtn, stopBtn, bindBtn, unbindBtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//實例化Button
startBtn = (Button)findViewById(R.id.startButton01);
stopBtn = (Button)findViewById(R.id.stopButton02);
bindBtn = (Button)findViewById(R.id.bindButton03);
unbindBtn = (Button)findViewById(R.id.unbindButton04);
//添加監聽器
startBtn.setOnClickListener(startListener);
stopBtn.setOnClickListener(stopListener);
bindBtn.setOnClickListener(bindListener);
unbindBtn.setOnClickListener(unBindListener);
}
//啓動Service監聽器
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
//設置Action屬性
intent.setAction("com.android.test.action.MY_SERVICE");
startService(intent);
}
};
//停止Service監聽器
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.android.test.action.MY_SERVICE");
stopService(intent);
}
};
//綁定Service監聽器
private OnClickListener bindListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.android.test.action.MY_SERVICE");
//綁定Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
};
//解除Service監聽器
private OnClickListener unBindListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.android.test.action.MY_SERVICE");
//解除綁定
unbindService(conn);
}
};
private ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("SERVICE", "連接成功!");
Toast.makeText(MainActivity.this, "連接成功!", Toast.LENGTH_LONG).show();
}
public void onServiceDisconnected(ComponentName name) {
Log.i("SERVICE", "斷開連接!");
Toast.makeText(MainActivity.this, "斷開連接!", Toast.LENGTH_LONG).show();
}
};
|
3:創建一個MyService.java繼承Service,覆蓋其聲明週期中的方法,並在各個方法中顯示信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.android.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
//可以返回null,通常返回一個aidl定義的接口
public IBinder onBind(Intent intent) {
Log.i("SERVICE", "onBind......");
Toast.makeText(MyService.this, "onBind......", Toast.LENGTH_LONG).show();
return null;
}
//Service創建時調用
public void onCreate() {
Log.i("SERVICE", "onCreate......");
}
//當客戶端調用startService()方法啓動Service時,該方法被調用
public void onStart(Intent intent, int startId) {
Log.i("SERVICE", "onStart......");
Toast.makeText(MyService.this, "onStart......", Toast.LENGTH_LONG).show();
}
//當Service不再使用時調用
public void onDestroy() {
Log.i("SERVICE", "onDestroy......");
Toast.makeText(MyService.this, "onDestroy......", Toast.LENGTH_LONG).show();
}
}
|
4:在AndroidManifest.xml配置文件中聲明Activity和Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="MyService">
<intent-filter>
<action android:name="com.android.test.action.MY_SERVICE"/>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
|
運行如圖:
本文出自 “Art_Hero” 博客,請務必保留此出處http://curran.blog.51cto.com/2788306/527777
轉載地址:http://disanji.net/2011/05/12/android-service-intent/