通过bind实现activity与service的交互

先点bind按钮实现onCreate,在点击start给service传值(get方法)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zzw.bind.MainActivity" >

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:text="start" />

    <Button
        android:id="@+id/bind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/start"
        android:layout_below="@+id/start"
        android:layout_marginTop="65dp"
        android:text="bind" />

    <Button
        android:id="@+id/dedao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bind"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="80dp"
        android:text="取值" />

</RelativeLayout>

布局

记住注册service

MainActivity:


package com.zzw.bind;

import com.zzw.bind.MyAppService.MyBinder;

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

public class MainActivity extends Activity {
    private ServiceConnection sc;
    private MyAppService myAppService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sc = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyBinder mBinder = (MyBinder) service;
                myAppService = mBinder.getService();
            }
        };
        findViewById(R.id.start).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startService();
            }
        });
        findViewById(R.id.bind).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                bindService();
            }
        });
        findViewById(R.id.dedao).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String str = myAppService.getServiceValue();
                Log.d("得到的值", str);
            }
        });

    }

    private void startService() {
        myAppService.setServiceValue("hello");

        Intent intent = new Intent(this, MyAppService.class);
        startService(intent);
    }

    private void bindService() {
        Intent intent = new Intent(this, MyAppService.class);
        bindService(intent, sc, Service.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(this, MyAppService.class);
        stopService(intent);
        Log.d("MainActivity", "onDestroy");
    }

}

Service:
package com.zzw.bind;

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

public class MyAppService extends Service {
    private String str;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyAppService","onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.str=str+",world";
        Log.d("onStartCommand",str);
        return super.onStartCommand(intent, flags, startId);
    }

    public String getServiceValue() {
        Log.d("getServiceValue", str);
        return str;
    }

    public void setServiceValue(String str) {
        Log.d("setServiceValue", str);
        this.str = str;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("onBind", "======");
        return new MyBinder();
    }

    public class MyBinder extends Binder {
        public MyAppService getService() {
            Log.d("getService", "====");
            return MyAppService.this;
        }
    }

    @Override
    public void onDestroy() {
        Log.d("MyAppService", "onDestroy");
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("MyAppService", "onUnbind");
        return super.onUnbind(intent);
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值