博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android--建立能与访问者进行相互通信的本地服务
阅读量:4691 次
发布时间:2019-06-09

本文共 3748 字,大约阅读时间需要 12 分钟。

笔记摘要:

    本地服务:访问者跟服务在同一个进程内。远程服务:访问者跟服务不在同一个进程内,因为通过startService()和stopService()启动关闭服务,

  适用于服务和访问者没有交互的情况。如果服务和访问者之间需要方法调用或者传递参数,则需要使用bindService()unbindService()方法启动关闭服务。

  这里就通过一个简单的查询示例来演示相互通信的本地服务

执行过程

    采用Context.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法,这个时候访问者和服务绑定在一起。

    如果访问者要与服务进行通行,那么,onBind()方法必须返回Ibinder对象。如果访问者退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法。

  如果调用bindSercice()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被重

  复调用)。如果访问者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind()---->onDestory()方法

使用IBundle实现客户端与服务端之间的服务:

当客户端调用bindService()方法启动服务,操作系统就会调用服务中的onBind方法,返回一个Ibinder对象,然后传给客户端,客户端就可以通过IBinder对象调用service中的方法

效果图:

代码体现:

AndroidMainfest.xml中添加服务

说明:bindService()方法需要的参数(服务,服务连接对象,操作标识),服务连接对象是一个接口,需要实现

package cn.xushuai.studentquery;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class StudentQuery extends Activity {	private EditText studentno;	private TextView resultView;	private StudentServiceConnection conn = new StudentServiceConnection();	private IStudent iStudent;    	@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_student_query);                studentno = (EditText)this.findViewById(R.id.studentno);        Button button = (Button) this.findViewById(R.id.button);        resultView = (TextView) this.findViewById(R.id.resultView);        button.setOnClickListener(new ButtonClickListener());                Intent service = new Intent(this,StudentService.class);        bindService(service, conn, BIND_AUTO_CREATE);//绑定后自动创建服务    }    //实现一个服务连接对象    private class StudentServiceConnection implements ServiceConnection{		public void onServiceConnected(ComponentName name, IBinder service) {			iStudent= (IStudent) service;		}		public void onServiceDisconnected(ComponentName name) {			iStudent = null;		}    	    }        //当Activity摧毁的时候,解除与服务的绑定    @Override	protected void onDestroy() {    	unbindService(conn);		super.onDestroy();	}	private final class ButtonClickListener implements View.OnClickListener{		public void onClick(View v) {			String no = studentno.getText().toString();			String name = iStudent.queryStudent(Integer.valueOf(no));			resultView.setText(name);		}    	    }}

说明:

    在业务类StudentService中定义提供服务的方法,由于是使用Binder实现访问者与服务者间的通信,所以在系统调用的onBind方法里返回一个Ibinder对象,

    使用该对象实现方法的调用,由于Ibinder是一个接口,这里自定义一个类StudentBinder,去继承已经实现Ibinder的类binder,然后返回该类的一个对象,我

    们在该类中定义方法,用于客户端调用,,这里将StudentBinder定义为private,所以通过一个接口IStudent来实现访问

package cn.xushuai.studentquery;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class StudentService extends Service {	private String[] names = {"张三","李四","王五"};	private IBinder binder = new StudentBinder();		public String query(int no){		if(no>0 && no<4){			return names[no-1];		}		return null;	}		@Override	public IBinder onBind(Intent intent) {		System.out.println("onBind...............");		return binder;	//向客户端返回IBinder接口的一个实现类	}		//通过Binder对象调用服务的方法	//IBinder为接口,这里继承一个已经实现IBinder的类,在这个类中定义一些方法,用于客户端调用	private class StudentBinder extends Binder implements IStudent{		public String queryStudent(int no) {			return query(no);	//直接调用query方法		}			}}

用于访问StudentBinder的接口

package cn.xushuai.studentquery;public interface IStudent {	public String queryStudent(int no);}

转载于:https://www.cnblogs.com/xushuai123/archive/2013/01/31/3634416.html

你可能感兴趣的文章