淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(四)

DEMO1:在Activity里声明一个回调方法,当service完成任务后,调用这个回调方法。

首先,我们先继承service,来创建服务,代码如下:

 1 package com.example.service;

 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 import android.widget.Button;

 9 

10 public class MyService extends Service {

11 

12     public static final String TAG = "MYSERVICE";

13     

14     @Override

15     public void onCreate() {

16         Log.i(TAG, "MyService-->onCreate");

17         super.onCreate();

18     }

19 

20     @Override

21     public void onDestroy() {

22         Log.i(TAG, "MyService-->onDestroy");

23         super.onDestroy();

24     }

25 

26     @Override

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

28         Log.i(TAG, "MyService-->onStartCommand");

29         return super.onStartCommand(intent, flags, startId);

30     }

31     private MyBinder binder = new MyBinder();

32     

33     public class MyBinder extends Binder implements ICalculator

34     {

35          public MyService getService() 

36          {

37              return MyService.this;

38          }

39          

40          @Override

41          public int add(int x, int y) {

42                 try {

43                     Thread.sleep(10000);

44                 } catch (InterruptedException e) {

45                     // TODO Auto-generated catch block

46                     e.printStackTrace();

47                 }

48                 return x + y;

49          }

50     }

51 

52     @Override

53     public IBinder onBind(Intent arg0) {

54         

55         return binder;

56     }

57 

58     

59 

60 }

其中,我定义了一个公共的接口

package com.example.service;



public interface ICalculator {

    int add(int x,int y);

}

 

主页面:MainActivity.java

 

package com.example.servicedemo;



import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



import com.example.service.ICalculator;

import com.example.service.MyService;





/*

 * bindService(intent,conn,flag)

 * Service:onCreate()

 * Service:onBind()

 * Activity:onServiceConnected()

 */

public class MainActivity extends Activity implements OnClickListener {

    private Button btnStartSrv,btnStopSrv,btnBindSrv;

    

    private ICalculator ical;



    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        btnStartSrv = (Button)findViewById(R.id.btnStartSrv);

        btnStopSrv = (Button)findViewById(R.id.btnStopSrv);

        btnBindSrv = (Button)findViewById(R.id.btnBindSrv);

        

        btnStartSrv.setOnClickListener(this);

        btnStopSrv.setOnClickListener(this);

        btnBindSrv.setOnClickListener(this);

        

        

        Intent intent=new Intent(MainActivity.this,MyService.class); 

        bindService(intent, conn, BIND_AUTO_CREATE);

    }

    

    @Override

    protected void onDestroy() {

        unbindService(conn);

        super.onDestroy();

    }



    @Override

    public void onClick(View view) {

        Intent service = new Intent(this, MyService.class);

        switch(view.getId())

        {

            case R.id.btnStartSrv:

            {

                

                startService(service);

                break;

            }

            case R.id.btnStopSrv:

            {

                stopService(service);

                break;

            }

            case R.id.btnBindSrv:

            {

                testSrv();

                Toast.makeText(this, "服务调用完了", 1).show();

                break;

            }

            default:

                break;

        }

    }

    

    ServiceConnection conn=new ServiceConnection(){



        @Override

        public void onServiceConnected(ComponentName arg0, IBinder service) {

            ical = (ICalculator) service;

        }



        @Override

        public void onServiceDisconnected(ComponentName arg0) {

            

        }

    };



    public void testSrv()

    {

        if(ical != null)

        {

            int x = ical.add(3, 5);

            Toast.makeText(this, String.valueOf(x), 1).show();

        }

    }







}

我故意在service里的方法,做了休眠10秒钟,当我们点击的BindSrv按钮时,过了10秒钟才弹出对话框,得到服务的运行结果。

淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(四)

所以,如果我在service中处理相对耗时的,就得在服务中另开一个线程。

转载请注明http://www.cnblogs.com/yushengbo,否则将追究版权责任!

 

你可能感兴趣的:(service)