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 ;
	}

}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值