Binder_service加法



package com.example.binder_service;

import com.example.binder_service.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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

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 onServiceConnected(ComponentName name, IBinder service) {
				MyBinder mBinder = (MyBinder)service;
				myAppService = mBinder.getService();
			}

			@Override
			public void onServiceDisconnected(ComponentName name) {
			
			}
		};
		
		Button start = (Button) findViewById(R.id.start);
		start.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startService();	
			}
		});
		
		Button bind=(Button) findViewById(R.id.bind);
		bind.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				bindService();
			}
		});
		
		Button getValue=(Button) findViewById(R.id.get);
		getValue.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int result = myAppService.getServiceIntValue();
				Log.d("服务获取的值", result+"");
			}
		});	
	}
	
	private void startService() {
		
		Intent intent = new Intent(this,MyAppService.class);
		startService(intent);
	}

	private void bindService() {
		Intent mIntent = new Intent(this,MyAppService.class);
		bindService(mIntent,sc,Service.BIND_AUTO_CREATE);
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		Intent intent = new Intent(this, MyAppService.class);
		stopService(intent);
	}
}



package com.example.binder_service;

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;
	
	private	int a=1,b=3,result=-1;
	
	@Override
	public void onCreate() {
		Log.d(this.getClass().getName(), "onCreate");
	}
	
	//需要Service处理的操作在此:
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		Log.d("onStartCommand", result+"");
		this.result=a+b;
		
		return super.onStartCommand(intent, flags, startId);
	}
	
	
	
	public int getServiceIntValue() {
		return result;
	}

	
	public void setServiceValue(int a,int b) {
		this.a=a;
		this.b=b;
	}

	@Override
	public IBinder onBind(Intent intent) {
		
		return new MyBinder();
	}
	public class MyBinder extends Binder{
		
		public MyAppService getService(){
			return MyAppService.this;
				
		}
	}

	@Override
	public boolean onUnbind(Intent intent) {
		
		return super.onUnbind(intent);
	}
	
	@Override
	public void onDestroy() {
			
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值