首页 > 代码库 > Activity向服务传递数据

Activity向服务传递数据

activity界面负责启动服务把数据打包,service获取数据,进行操作。具体demo如下:

package com.example.android_service_trance;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.android_service_trance.MyService.LocalBinder;

@SuppressLint("Recycle")
public class MainActivity extends Activity {
	
	

	private Button bindService=null;
	private Button callService=null;
	private Button communicationService=null;
	private TextView tv=null;
	private boolean flag=false;//默认为不绑定
	private MyService myService=null;
	private LocalBinder binder;//服务里的对象
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bindService=(Button)this.findViewById(R.id.button1);
		callService=(Button)this.findViewById(R.id.button2);
		communicationService=(Button)this.findViewById(R.id.button3);
		tv=(TextView)this.findViewById(R.id.textView1);
		bindService.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
					//获得意图
					Intent intent=new Intent(MainActivity.this,MyService.class);
					//绑定服务
					bindService(intent, connection, Context.BIND_AUTO_CREATE);
				
			
				
			}
		});
		callService.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(flag){
					int result=myService.getRandom();
					tv.setText("<<<<<<<<"+result);
				}
				
			}
		});
		communicationService.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//往service中传递值的对象
				Parcel data=http://www.mamicode.com/Parcel.obtain();>
service端:

/**
 *Version:
 *author:YangQuanqing
 *Data:
 */
package com.example.android_service_trance;

import java.util.Random;

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

/**
 * @author YangQuanqing yqq
 *
 */
public class MyService extends Service {

	//定义一个随机数用于测试
	private Random random=new Random();
	private LocalBinder lb=new LocalBinder();
	//获得当前类的实例
	public class LocalBinder extends Binder{
		
		

		public MyService getService()
		{
			return MyService.this;
		}
		
		@Override
		protected boolean onTransact(int code, Parcel data, Parcel reply,
				int flags) throws RemoteException {
			//Activity里获取数据
			Log.i("SERVICE<<<<<<<<",data.readString());
			Log.i("SERVICE<<<<<<<<<",data.readInt()+"");
			reply.writeString("小名");
			reply.writeInt(1990);
			
			return super.onTransact(code, data, reply, flags);
		}
		
		
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return lb;
	}
	//获得一个随机数
	public int getRandom()
	{
		
		return random.nextInt(22);
	}
	

}


Activity向服务传递数据