开启服务的方式:(2)绑定式开启服务

本文介绍了一个具体的Android应用程序示例,展示了如何通过绑定服务来实现Activity与Service之间的交互。主要讲解了ServiceConnection接口的使用方法,包括如何在Activity中定义并实现ServiceConnection,以及如何通过Intent绑定和解绑Service。
public class MainActivity extends Activity implements OnClickListener {

    private Button button1, button2, button3;
    private Intent service;
    private MyIBinder service1;
    private ServiceConnection conn = new ServiceConnection() {

        public void onServiceDisconnected(ComponentName name) {
            Log.i("myTag", "与service没有连接成功--->onServiceDisconnected");

        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            MainActivity.this.service1 = (MyIBinder) service;
            Log.i("myTag", "与service连接成功--->onServiceConnected");
            Log.i("myTag", "service.toString()---->" + service.toString());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.button1:
            service = new Intent(MainActivity.this, MyBindService.class);
            /**
             * 参数一:该参数通过intent指定要启动的service
             * 
             * 参数二:是一个ServiceConnection对象,用于监听访问者与service之间的连接情况
             * ,如果连接成功调用onServiceConnected(ComponentName name, IBinder
             * service)方法, 如果连接不成功调用onServiceDisconnected(ComponentName name)方法
             * 
             * 参数三:指定绑定是是否自动创建service
             * (如果service还未创建),该参数可以指定为0(不自动创建),或者BIND_AUTO_CREATE(自动创建)
             * */
            bindService(service, conn, BIND_AUTO_CREATE);
            break;

        case R.id.button2:
            unbindService(conn);

            break;

        case R.id.button3:
            service1.geiQianBanShi();
            break;

        }

    }

}
public class MyBindService extends Service {

    private MyIBinder myIBinder = new MyIBinder();
    // 绑定服务时调用此方法
    public IBinder onBind(Intent intent) {
        Log.i("myTag", "bind serviec--->onBind");
        Log.i("myTag", "myIBinder.tostring--->" + myIBinder.toString());
        return myIBinder;       
    }

    // 解绑服务时调用此方法
    public boolean onUnbind(Intent intent) {
        Log.i("myTag", "bind serviec--->onUnbind");
        return super.onUnbind(intent);
    }

    public void onCreate() {
        Log.i("myTag", "bind serviec--->onCreate");
        super.onCreate();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("myTag", "bind serviec--->onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    public void onDestroy() {
        Log.i("myTag", "bind serviec--->onDestroy");
        super.onDestroy();
    }

    /**
     * 服务的方法
     * 
     * */
    public void banShi(int money) {
        if (money < 1000) {
            Log.i("myTag", "--->钱不够");
        } else {
            Log.i("myTag", "--->事情办好了");
        }
    }

    /**
     * 创建一类,实现了IBinder接口,将数据返回给activity
     * */
    class MyIBinder extends Binder {
        public void geiQianBanShi() {
            banShi(500);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值