Android学习笔记-Service 基本用法

来源:互联网 发布:淘宝优惠券赚佣金 编辑:程序博客网 时间:2024/06/06 02:35

Service 基本用法

1. 定义一个service

1.1 创建service类,定义各种操作。

package com.prohan.atmoparse.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service {    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onCreate() {        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();    }}

1.2 在AnidroidMainfest.xml中注册。

<service android:name=".service.MyService"></service>

2. 启动和停止服务

new intent(this,MyService.class);//将要启动的服务StartService(Intent intent);//启动服务StopService(Intent intent);//停止服务

绑定服务,在Activity中操作Service

新建一个类Downloadbinder 继承自Binder,在里面提供我们需要指挥服务的方法(要做的工作);并且在MyService类中创建DownloadBinder对象,通过onBinder方法返回实例对象;

public class MyService extends Service {    private Downloadbinder mbinder = new Downloadbinder();    class Downloadbinder extends Binder {        public void download() {            Log.d("Myservice", "开始下载");        }        public int getprogress() {            Log.d("Myservice", "下载进度。。。");            return 0;        }    }    @Override    public IBinder onBind(Intent intent) {        return mbinder;    }}

接下来考虑如何调用downloadbinder中的方法。

当一个activity和一个Service绑定了之后,就可以调用该服务里binder提供的方法啦。

1、创建一个serviceConnection的匿名类,重写onServiceConnected()方法和ons erviceDisconnected()方法,这两个方法分别在成功绑定和解绑的时候调用。
2、在 onServiceConnected()中通过向下转型得到的一个DownLoadbinder的实例,有了这个实例就可以指定服务,使用Downloadbinder中的方法。
3、是用bindService();来将Activity与Service进行绑定。bindService的三个参数,第一个参数是一个以Service为目地的Intent,第二个参数是ServiceConection的实例,第三个参数为标志位。

public class MainActivity extends Activity implements OnClickListener {    private Button startServiceButton;    private Button stopServiceButton;    private Button bindButton;    private Button unbindButton;    private MyService.Downloadbinder downloadbinder;    private ServiceConnection conn = new ServiceConnection() {        @Override        public void onServiceDisconnected(ComponentName name) {        }        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            downloadbinder = (MyService.Downloadbinder) service;            downloadbinder.download();            downloadbinder.getprogress();        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        startServiceButton = (Button) findViewById(R.id.startService);        stopServiceButton = (Button) findViewById(R.id.stopService);        bindButton = (Button) findViewById(R.id.bindService);        unbindButton = (Button) findViewById(R.id.unbindService);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.bindService:            Intent service = new Intent(this, MyService.class);            bindService(service, conn, BIND_AUTO_CREATE);            break;        case R.id.unbindService:            unbindService(conn);            break;        default:            break;        }    }}
1 0