天天看點

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

}
           

繼續閱讀