绑定启动Service

package com.example.immediate.mybindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
    private Button mButtonBind;
    private MyBindService myBindService;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d("onbind", "得到service对象");
            if (iBinder instanceof MyBindService.MyBinder){
                myBindService = ((MyBindService.MyBinder) iBinder).getService();//得到MyBindService的对象
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //此方法不是解除绑定时运行的,是在绑定的service因为内存不足时被回收导致连接失败
            Log.d("onbind", "service断开连接");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonBind = (Button) findViewById(R.id.button_bind);
        mButtonBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /**
                 * 三个参数:1、Intent 要启动那个service
                 *          2、ServiceConnection,连接Activity和绑定的service,得到绑定的service
                 *          3、启动service的方式
                 */
                Intent intent = new Intent(getApplicationContext(),MyBindService.class);
                bindService(intent,connection ,BIND_AUTO_CREATE);
                Log.d("onbind", "开始绑定Service");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}
package com.example.immediate.mybindservice;

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

/**
 * Created by Administrator on 2015/11/5.
 */
public class MyBindService extends Service {
    class MyBinder extends Binder{

        public MyBindService getService() {
            Log.d("onbind","返回service对象");
            return MyBindService.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        //Android中提供了一个实现IBinder接口的类Binder
        Log.d("onbind","绑定 onBind");
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("onbind", "Service的创建onCreate");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("onbind","Service解除绑定onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.d("onbind","Service关闭onDestroy");
        super.onDestroy();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值