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

时间:2021-04-11 04:36:34

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

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

 package com.example.service;

 import android.app.Service;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.IBinder;
 import android.util.Log;
 import android.widget.Button;

 public class MyService extends Service {

     public static final String TAG = "MYSERVICE";

     @Override
     public void onCreate() {
         Log.i(TAG, "MyService-->onCreate");
         super.onCreate();
     }

     @Override
     public void onDestroy() {
         Log.i(TAG, "MyService-->onDestroy");
         super.onDestroy();
     }

     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         Log.i(TAG, "MyService-->onStartCommand");
         return super.onStartCommand(intent, flags, startId);
     }
     private MyBinder binder = new MyBinder();

     public class MyBinder extends Binder implements ICalculator
     {
          public MyService getService()
          {
              return MyService.this;
          }

          @Override
          public int add(int x, int y) {
                 try {
                     Thread.sleep(10000);
                 } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 return x + y;
          }
     }

     @Override
     public IBinder onBind(Intent arg0) {

         return binder;
     }

 }

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

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,否则将追究版权责任!