Android Service

如何在Activity中调用服务服务中的方法呢,下面咱们用一个例子简单的说一下html

 

需求:服务中有一个方法,返回值为String 内容为“张三”,如今咱们经过调用服务中的方法把“张三”显示在主Activity上。android

新建一个服务类MyService,继承Service

web

view sourceprint?app

01.package cn.cbd.service;ide

02.import android.app.Service;this

03.import android.content.Intent;spa

04.import android.os.Binder;.net

05.import android.os.IBinder;code

06.public class MyService extends Serviceorm

07.{

08.@Override

09.public void onCreate()

10.{

11.super.onCreate();

12.}

13.@Override

14.public IBinder onBind(Intent intent)

15.{

16.return new MyBinder();

17.}

18.@Override

19.public void onDestroy()

20.{

21.super.onDestroy();

22.}

23.public class MyBinder extends Binder

24.{

25.public String getname()

26.{

27.return getName();

28.}

29.}

30./**

31.* 在服务中自定义一个方法,咱们要在activity中调用这个方法

32.*

33.* @return

34.*/

35.public String getName()

36.{

37.return "张三";

38.}

39.}

在上面咱们看到,getName()这个方法是咱们在Service中定义的,如今咱们要在主Activity中调用这个方法。怎么调用呢,上面MyService中有一个onBind(Intent intent)方法 返回值类型为IBinder activity和service就是经过这个方法来进行通讯。 因此呢 咱们为了方便起见,就自定义了一个类MyBinder继承Binder ,在MyBinder类中定义一个方法getname(),返回getName()的值。

Service也是android四大组件之一,因此呢,不要忘记在清单文件中对其进行配置

 

 

下面咱们看Activity这一面:

view sourceprint?

01.package cn.cbd.service;

02.import android.app.Activity;

03.import android.content.ComponentName;

04.import android.content.Context;

05.import android.content.Intent;

06.import android.content.ServiceConnection;

07.import android.os.Bundle;

08.import android.os.IBinder;

09.import android.view.View;

10.import android.view.View.OnClickListener;

11.import android.widget.Button;

12.import android.widget.TextView;

13.public class ServiceTestActivity extends Activity

14.{

15.private Button btn_bindService;

16.private TextView tv_show;

17.private MyServiceConnection conn;

18.private MyService.MyBinder myBinder;

19.public void onCreate(Bundle savedInstanceState)

20.{

21.super.onCreate(savedInstanceState);

22.setContentView(R.layout.main);

23.btn_bindService = (Button) findViewById(R.id.btn_bindService);

24.tv_show = (TextView) findViewById(R.id.tv_show);

25.btn_bindService.setOnClickListener(new OnClickListener()

26.{

27.public void onClick(View v)

28.{

29.// 开启服务

30.Intent service = new Intent(ServiceTestActivity.this,

31.MyService.class);

32.conn = new MyServiceConnection();

33.// startService(service)只能单纯的开启一个服务,要想调用服务服务中的方法,必须用bindService和unbindService

34.// startService(service);

35.bindService(service, conn, Context.BIND_AUTO_CREATE);

36.}

37.});

38.}

39./**

40.* 自定义一个类,实现ServiceConnection接口,并重写其两个方法

41.* @author Administrator

42.*

43.*/

44.public class MyServiceConnection implements ServiceConnection

45.{

46.//当绑定服务成功的时候会调用此方法

47.public void onServiceConnected(ComponentName name, IBinder service)

48.{

49.//获得MyService.MyBinder对象,咱们经过这个对象来操做服务中的方法

50.myBinder = (MyService.MyBinder) service;

51.//调用服务中的getname()方法并把值设置到TextView上进行显示

52.tv_show.setText(myBinder.getname());

53.}

54.public void onServiceDisconnected(ComponentName name)

55.{

56.}

57.}

58.@Override

59.protected void onDestroy()

60.{

61.// 在程序销毁的时候要对服务解除绑定

62.unbindService(conn);

63.super.onDestroy();

64.}

65.}

相关文章
相关标签/搜索