1 package fry;
2
3 import java.io.FileDescriptor;
4
5 import android.app.Service;
6 import android.content.Intent;
7 import android.os.Binder;
8 import android.os.IBinder;
9 import android.os.IInterface;
10 import android.os.Parcel;
11 import android.os.RemoteException;
12 import android.util.Log;
13
14 public class myService extends Service{
15
16 /**
17 * 当绑定这个服务的时候调用
18 */
19 @Override
20 public IBinder onBind(Intent arg0) {
21 Log.d("fanfan", "onBind");
22 return new MyServiceBinder();
23 }
24 /**
25 * 当解除绑定这个服务的时候调用
26 */
27 @Override
28 public boolean onUnbind(Intent intent) {
29 Log.d("fanfan", "onUnbind");
30 return super.onUnbind(intent);
31 }
32 /**
33 * 当重新绑定这个服务的时候调用
34 */
35 @Override
36 public void onRebind(Intent intent) {
37 Log.d("fanfan", "onRebind");
38 super.onRebind(intent);
39 }
40
41 /**
42 * service被创建后调用
43 */
44 @Override
45 public void onCreate() {
46 Log.d("fanfan", "onCreate");
47 super.onCreate();
48 }
49
50 /**
51 * service被start后调用
52 */
53 @Override
54 public int onStartCommand(Intent intent, int flags, int startId) {
55 Log.d("fanfan", "onStartCommand");
56 return super.onStartCommand(intent, flags, startId);
57 }
58
59 /**
60 * service被停止后调用
61 */
62 @Override
63 public void onDestroy() {
64 Log.d("fanfan", "onDestroy");
65 super.onDestroy();
66 }
67
68 public void show(){
69 Log.d("fanfan", "show");
70 }
71
72 /**
73 * 中介
74 * @author Fry
75 *
76 */
77 public class MyServiceBinder extends Binder{
78 public void show(){
79 myService.this.show();
80 }
81
82 }
83
84
85 }
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2 package="com.example.bindservice"
3 android:versionCode="1"
4 android:versionName="1.0" >
5
6 <uses-sdk
7 android:minSdkVersion="8"
8 android:targetSdkVersion="19" />
9
10 <application
11 android:allowBackup="true"
12 android:icon="@drawable/ic_launcher"
13 android:label="@string/app_name"
14 android:theme="@style/AppTheme" >
15 <activity
16 android:name="fry.MainActivity"
17 android:label="@string/app_name" >
18 <intent-filter>
19 <action android:name="android.intent.action.MAIN" />
20
21 <category android:name="android.intent.category.LAUNCHER" />
22 </intent-filter>
23 </activity>
24 <activity android:name="fry.Activity01" android:exported="true"></activity>
25
26 <service android:name="fry.myService">
27
28 </service>
29
30 </application>
31
32 </manifest>
1 package fry;
2
3 import com.example.bindservice.R;
4
5 import fry.myService.MyServiceBinder;
6 import android.app.Activity;
7 import android.content.ComponentName;
8 import android.content.Context;
9 import android.content.Intent;
10 import