首页 > 代码库 > Service传值

Service传值

Service 的传值

直接借助Intent 传值,不用说了,很简单,这也是我知道的唯一一个启动式Service与Activity之间的传值,哦对了还可以借助广播进行传值。其 余的传值都是讲的绑定式Service的传值。

绑定式Service可以调用Service对象,调用Service里实现的方法。(写Service记住一定要注册)

Parcel可以通过IBinder传值

Activity的代码

package com.example.servicecomunicateone;

import com.example.servicecomunicateone.LocalService.MyIBinder;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private boolean bound;
	private MyIBinder binder;
	private ServiceConnection conn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			bound = false;
			
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			bound=true;
			//获取MyIBinder
			//借助MyIBinder进行传值
			 binder =(MyIBinder) service;
			
			
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//Toast.makeText(getApplicationContext(), "开始",1).show();
		System.out.println("------------->>开始");
	
		Button btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Parcel parcel =Parcel.obtain();
				parcel.writeString("我该怎么办?");
				Parcel reply =Parcel.obtain();
				try {
					//传值
					binder.transact(IBinder.LAST_CALL_TRANSACTION, parcel, reply, 0);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				//输出接受数据
				System.out.println("-------->>从Service获得的值:"+reply.readString());
				
			}
		});
		
		
	}
     @Override
    protected void onStart() {
      //绑定Service
       Intent intent = new Intent(this,LocalService.class);
       //intent传值
       intent.putExtra("name", "坚强");
       bindService(intent, conn, Context.BIND_AUTO_CREATE);
    	super.onStart();
    }
	@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;
	}

}

Service

package com.example.servicecomunicateone;

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

public class LocalService extends Service { 
	private MyIBinder binder = new MyIBinder();
	class MyIBinder extends  Binder{
		public LocalService getService(){
			return LocalService.this;
			
		}
	   //重写传值方法
		protected boolean onTransact(int code, Parcel data, Parcel reply,
				int flags) throws RemoteException {
			System.out.println("------->>从Activity传来的值:"+data.readString()); 
			reply.writeString("要坚强,要自立,要自尊");
			
			return super.onTransact(code, data, reply, flags);
		}
		
	}
	

	
	public IBinder onBind(Intent intent) {
		
		System.out.println("-----intent传值->>"+intent.getStringExtra("name"));
		
		
		return binder ;
	}

}






Service传值