初识Service

一.启动Service–startService
步骤,方法
1.
准备工作

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:onClick="doclick"
        android:text="startService" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:onClick="doclick"
        android:text="stopService" />

2.创建一个类继承Service实现生命周期中的四个方法

@Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

@Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

当点击的时候利用接口的方法启动服务

public void doclick(View v){
       switch (v.getId()) {
    case R.id.button1:
    //注意,这就是普通的启动service的方法
    //此处service1声明在头部为Intent对象
        service1 = new Intent(MainActivity.this,MyService.class);
        startService(service1);
        break;
    case R.id.button2:      
        stopService(service1);
        break;
}

二.绑定Service–bindService
前两步都是一样的
继承service时实现的方法不一样

1.准备工作

 <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:onClick="doclick"
        android:text="bind" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button3"
        android:onClick="doclick"
        android:text="unbind" />

2.创建一个类继承Service实现生命周期中的四个方法

@Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }
    //因为我们要试着把service对象闯入activity中用,而binder类中封装了这方面的方法,所以我们新建一个这样的类继承binder并返回一个binder对象
    public class MyBinder extends Binder{
        public MyBindService getService(){
            return MyBindService.this;
        }
    }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        //在这new一个binder对象并返回,那么只要service已绑定,就会有一个当前帮定的服务对象传出
        return new MyBinder();
    }
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onUnbind(intent);
    }

3.创建ServiceConnection对象,第一个方法防止服务没解绑的意外推出,第二个方法连接服务之后执行的方法,我们之所有要用类还有一个原因就是在下面要bindService()和unbind()的时候有一个connection的对象参数

ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder binder) {
            // TODO Auto-generated method stub
            service = ((MyBinder)binder).getService();
        }
    };

4.添加事件

case R.id.button3:
        service2 = new Intent(MainActivity.this,MyBindService.class);
        bindService(service2, conn, Service.BIND_AUTO_CREATE);
        break;
    case R.id.button4:
        unbindService(conn);
        break;

还可以做一下兼容
当activity退出的时候自动销毁或解绑service

protected void onDestroy() {
    // TODO Auto-generated method stub
        unbindService(conn);
    super.onDestroy();
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值