service与activity通信 之 bindService

此文是在网友文章基础上经过修改得到的,在此处谢谢慷慨的网友们。


本文是只是localService与activity通信

思路很简单,是这样的:在localservice中,有一个不断累加的整数i,在activity中启动service(bindService),然后把service中的当前i值返回给acitivity。

代码如下:

LocalService:

<pre name="code" class="java">public class LocalService extends Service{
	int i ; //需要返回的i值
	
	private final IBinder myBinder = new LocalBinder();
     public class LocalBinder extends Binder{  //用Binder的子类,返回service实例
		LocalService getService(){
			return LocalService.this;
		}
	}

	@Override
	public void onCreate() {  //启动线程
		MyThread mt = new MyThread();
		Thread t= new Thread(mt);
		t.start();
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {   //通过返回myBinder,activity可以得到Service的实例,这样就可以调用service中的public方法了 
		// TODO Auto-generated method stub
		return myBinder;
	}
	
	class MyThread implements Runnable{
		@Override
		public void run() {
			getNum();  //调用函数
		}
	}
	
	public void getNum(){//在后台中一直运行,不断更新i的值
		while (true) {
			i++;
			Log.d("abc--i",String.valueOf(i));
		}
	}
	
	public int getNumber() {//提供public方法,可以在activity中调用,返回当前的i值
		return i;
	}

}


 以下是acitivity代码: 

public class MainActivity extends Activity {
    LocalService mService;    // Service对象
    boolean ifBound = false;  //是否绑定成功

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

    @Override
    protected void onStart() {
        super.onStart();
        // 绑定Service,绑定以后就会调用conn里的onServiceConnected方法
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 解绑Service
        if (ifBound) {
            unbindService(conn);
            ifBound = false;
        }
    }

    public void onButtonClick(View v) {
        if (ifBound) {
            // 用Service的对象,去读i的值
            int num = mService.getNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }

    //  Called when a connection to the Service has been established, 
    //with the IBinder of the communication channel to the Service.
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // 已经绑定了LocalService,强转IBinder对象,调用方法得到LocalService对象
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            ifBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            ifBound = false;
        }
    };
}


界面简单,只有一个button ,点击调用onButtonClick方法,获取i值。
例子很简单,虽然是新手,希望跟大家交流,不断成长。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值