绑定本地Service并与之通信

绑定Service需要调用

public boolean bindService (Intent service, ServiceConnection conn, int flags);

传入一个ServiceConnection 对象,该对象是一个接口,实例化时需要实现该接口,它的作用就是获得Service的IBinder对象,通过IBinder对象可以实现与Service的通信。

Service的的代码:

 1 package com.example.servicetest;

 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 public class BindServiceTest extends Service {

10     private int count = 0;

11     private boolean quit = false;

12     private MyBinder mBinder = new MyBinder();

13 

14     public class MyBinder extends Binder {

15         public int count() {

16             return count;

17         }

18     }

19 

20     @Override

21     public IBinder onBind(Intent arg0) {

22         Log.i("csx", "onBind");

23         return mBinder;

24     }

25 

26     @Override

27     public void onCreate() {

28 

29         super.onCreate();

30         Log.i("csx", "onCreate");

31         new Thread() {

32 

33             @Override

34             public void run() {

35                 while (!quit) {

36                     try {

37                         Thread.sleep(1000);

38                     } catch (InterruptedException e) {

39 

40                         e.printStackTrace();

41                     }

42                     count++;

43 

44                 }

45 

46             }

47 

48         }.start();

49     }

50 

51     @Override

52     public int onStartCommand(Intent intent, int flags, int startId) {

53         Log.i("csx", "onStartCommand");

54         return START_STICKY;

55     }

56 

57     @Override

58     public boolean onUnbind(Intent intent) {

59         Log.i("csx", "onUnbind");

60         return true;

61     }

62 

63     @Override

64     public void onDestroy() {

65         super.onDestroy();

66         this.quit = true;

67         Log.i("csx", "onDestroy");

68     }

69 

70 }

组件的代码:

 1 package com.example.servicetest;

 2 

 3 import com.example.servicetest.BindServiceTest.MyBinder;

 4 

 5 import android.content.ComponentName;

 6 import android.content.Intent;

 7 import android.content.ServiceConnection;

 8 import android.os.Bundle;

 9 import android.os.IBinder;

10 import android.support.v7.app.ActionBarActivity;

11 import android.util.Log;

12 import android.view.View;

13 import android.view.View.OnClickListener;

14 import android.widget.Button;

15 import android.widget.Toast;

16 

17 public class MainActivity extends ActionBarActivity {

18     public static final String SERVICE_ACTION = "com.example.servicetest.BindServiceTest";

19     Button bindButton, unbindButton, stateButton;

20     ServiceConnection conn;

21     BindServiceTest.MyBinder mBinder;

22     boolean isBind = false;

23 

24     @Override

25     protected void onCreate(Bundle savedInstanceState) {

26         super.onCreate(savedInstanceState);

27         setContentView(R.layout.activity_main);

28         bindButton = (Button) findViewById(R.id.button_bind);

29         unbindButton = (Button) findViewById(R.id.button_unbind);

30         stateButton = (Button) findViewById(R.id.button_get_state);

31 

32         conn = new ServiceConnection() {

33 

34             @Override

35             public void onServiceConnected(ComponentName name, IBinder service) {

36                 Log.i("csx", "onServiceConnected");

37                 mBinder = (MyBinder) service; // 连接service之后将service中实现的Binder返回给本地声明的Binder

38 

39             }

40 

41             @Override

42             public void onServiceDisconnected(ComponentName name) {

43                 Log.i("csx", "onServiceDisconnected");// 意外断开连接会调用该函数

44 

45             }

46 

47         };

48 

49         bindButton.setOnClickListener(new OnClickListener() {

50 

51             @Override

52             public void onClick(View v) {

53                 Intent service = new Intent();

54                 service.setAction(SERVICE_ACTION);

55                 bindService(service, conn, BIND_AUTO_CREATE);// 绑定服务

56                 isBind = true;

57 

58             }

59         });

60 

61         unbindButton.setOnClickListener(new OnClickListener() {

62 

63             @Override

64             public void onClick(View v) {

65                 if (!isBind) {

66                     Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();

67                     return;

68 

69                 }

70 

71                 unbindService(conn);// 解除绑定服务

72                 isBind = false;

73 

74             }

75         });

76 

77         stateButton.setOnClickListener(new OnClickListener() {

78 

79             @Override

80             public void onClick(View v) {

81                 if (!isBind) {

82                     Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();

83                     return;

84 

85                 }

86                 Toast.makeText(MainActivity.this, "" + mBinder.count(), Toast.LENGTH_SHORT).show();// 通过mBinder获取service内部的数据

87 

88             }

89         });

90 

91     }

92 }

 

你可能感兴趣的:(service)