码迷,mamicode.com
首页 > 其他好文 > 详细

Activity向服务传递数据

时间:2014-11-19 15:58:36      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:android   数据   服务   

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=Parcel.obtain();
				data.writeInt(10086);
				data.writeString("李华");
				Parcel reply=Parcel.obtain();
				try {
					
					binder.transact(IBinder.LAST_CALL_TRANSACTION, data, reply, 0);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//从service里读数据
				Log.i("Main<<<<<<<<",reply.readString());
				Log.i("Main<<<<<<<<<",reply.readInt()+"");
			}
		});
		
	}

	//解除绑定
	@Override
	protected void onStop() {
		
		super.onStop();
		if(flag)
		{
			//解除绑定
			unbindService(connection);
			flag=false;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
	
	
	//连接activity和service的桥梁
	private ServiceConnection connection=new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder iBinder) {
			//连接
		
			
				//获得LocalBinder 
				 binder=(LocalBinder)iBinder;
				//获得MyService实例
				myService=binder.getService();
			
			flag=true;
			
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			//不连接
		
		
				flag=false;
		
			
			
		}};
		
}

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向服务传递数据

标签:android   数据   服务   

原文地址:http://blog.csdn.net/u014600432/article/details/41282401

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!