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.}

相關文章
相關標籤/搜索