博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Service bound(三)
阅读量:4628 次
发布时间:2019-06-09

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

service 绑定有三种实现方式:

1. 直接继承Binder类实现。

      条件: 同一应用,同一进程

2. 使用Messenger实现。

      条件:要在不同的进程间通信,这种方式不用考虑线程安全性。(单线程操作时使用)

3. 使用AIDL实现。

      条件:要在不同的进程间通信,并且需要多线程处理。要考虑线程之间的安全性。

使用AIDL实现:

三大基本步骤

  • 创建.aidl文件
  • 实现接口
  • 公开接口

创建.aidl文件

  • 方法定义有0个或者多个参数,可以返回一个值或者是void.
  • 方法中不是基本类型的参数,需要在方法参数前面加入in , out or inout
  • 包含在.aidl中所有的注释在IBinder接口中都会生成(除了在import和package之前的注释)
  • 仅仅支持方法,不支持静态的成员变量。
package com.hualu.servicemy;import com.hualu.servicemy.Book;interface IRemoteService{		int getPID() ;		void basicInt(int i) ;		void basicByte(byte b) ;		void basicLong(long l) ;		void basicDouble(double d) ;		void basicFloat(float f) ;		void basicString(String s) ;		void basicBoolean(boolean b) ;		void basicCharSequence(char c) ;		void basicList(inout List
l) ; void basicMap(in Map m) ; Book getBook() ; }

实现接口

  • 不能保证是从主线程里发起的调用,因此在使用的时候,需要考虑多线程启动和保证service运行时的线程安全性。
  • 默认情况,远程调用是同步的。
  • Service不会返回任何开发者自己抛出的异常到调用者。

package com.hualu.servicemy;import java.util.List;import java.util.Map;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.Process;import android.os.RemoteException;public class RemoteService extends Service {	@Override	public IBinder onBind(Intent intent) {		return iBinder;	}	private final IRemoteService.Stub iBinder = new IRemoteService.Stub() { //实现接口				@Override		public int getPID() throws RemoteException {			return  Process.myPid();		}				@Override		public Book getBook() throws RemoteException {			Book book = new Book() ;			book.setName("心善") ;			return book;		}				@Override		public void basicString(String s) throws RemoteException {			System.out.println("string = "+s);					}				@Override		public void basicMap(Map m) throws RemoteException {			System.out.println("Map size = "+m.size());					}				@Override		public void basicLong(long l) throws RemoteException {						System.out.println("long = "+l);		}				@Override		public void basicList(List
l) throws RemoteException { System.out.println("List size = "+l.size()); } @Override public void basicInt(int i) throws RemoteException { System.out.println("int = "+i); } @Override public void basicFloat(float f) throws RemoteException { System.out.println("float = "+f); } @Override public void basicDouble(double d) throws RemoteException { System.out.println("double = "+d); } @Override public void basicCharSequence(char c) throws RemoteException { System.out.println("char = "+c); } @Override public void basicByte(byte b) throws RemoteException { } @Override public void basicBoolean(boolean b) throws RemoteException { } }; }
公开接口

package com.hualu.servicemy;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;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.RemoteException;import android.view.View;import android.view.View.OnClickListener;import com.hualu.serviceexample.R;public class RemoteActivity extends Activity {	private IRemoteService  remoteService ;	private boolean bindFlag = false ;		@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.remote_service) ;		findViewById(R.id.remote).setOnClickListener(l) ;	}		private OnClickListener l = new OnClickListener(){		@Override		public void onClick(View v) {			if(remoteService != null){				try {					remoteService.basicByte((byte)4) ;					remoteService.basicBoolean(true) ;					remoteService.basicInt(132) ;					remoteService.basicLong(146) ;					remoteService.basicFloat(83646.3f) ;					remoteService.basicDouble(2.12) ;					remoteService.basicString("remoteService") ;					remoteService.basicCharSequence('r') ;					List
l = new ArrayList
() ; l.add("Remote") ; l.add("Service") ; remoteService.basicList(l) ; Map
m = new HashMap
(); m.put("a", "a") ; remoteService.basicMap(m) ; Book b = remoteService.getBook() ; System.out.println(b.getName()); } catch (RemoteException e) { e.printStackTrace(); } } } } ; @Override protected void onStart() { super.onStart(); binderService() ; } @Override protected void onStop() { super.onStop(); unBindService() ; remoteService = null ; } private ServiceConnection conn = new ServiceConnection(){ //公开接口 @Override public void onServiceConnected(ComponentName name, IBinder service) { remoteService = IRemoteService.Stub.asInterface(service) ; bindFlag = true ; } @Override public void onServiceDisconnected(ComponentName name) { remoteService = null ; bindFlag = false ; } } ; private void binderService(){ bindService(new Intent(RemoteService.class.getName()), conn, Context.BIND_AUTO_CREATE) ; } private void unBindService(){ unbindService(conn) ; bindFlag = false ; conn = null ; } }

在aidl文件传递对象:

  1. 创建实现Parcelable接口的类。
  2. 实现 , 将对象目前的状态写到Pacel中。
  3. 增加一个名字为CREATOR的静态的成员变量,这个CREATOR实现了Parcelable.Creator接口。
  4. 最终,创建一个.aidl文件,声明你的parcelable类。
Book.aidl
package com.hualu.servicemy;parcelable Book ;
Book.java

package com.hualu.servicemy;import android.os.Parcel;import android.os.Parcelable;public class Book implements Parcelable {	private String name ;		@Override	public int describeContents() {		return 0;	}		public Book(){}	    private Book(Parcel in) {        readFromParcel(in);    }	@Override	public void writeToParcel(Parcel dest, int flags) {		dest.writeString(name) ;	}		public void readFromParcel(Parcel in){		name = in.readString() ;	}			public final static Parcelable.Creator
CREATOR = new Parcelable.Creator
(){ @Override public Book createFromParcel(Parcel source) { return new Book(source); } @Override public Book[] newArray(int size) { return new Book[size]; } } ; public void setName(String name){ this.name = name ; } public String getName(){ return this.name ; } }
Layout文件:

Manifest文件中定义的Activity和service
:

运行结果:

公开接口

转载于:https://www.cnblogs.com/java20130722/archive/2013/04/08/3207348.html

你可能感兴趣的文章
【2019/4/30】周进度报告
查看>>
.net程序员面试题
查看>>
团队分数分配方法——BY 李栋
查看>>
docker获取镜像很慢解决办法
查看>>
学习-现代交换原理与通信技术
查看>>
【编程题目】左旋转字符串 ☆
查看>>
SQL Server 2008 R2如何开启数据库的远程连接
查看>>
笔记一:python安装和执行
查看>>
关于字符串的分割问题
查看>>
Tornado 类与类组合降低耦合
查看>>
2009 Competition Highlights by ICPC Live
查看>>
ssh远程操作服务器
查看>>
树莓派Android Things物联网开发:创建一个Things项目
查看>>
GIT使用方法
查看>>
第三阶段 10_JavaWeb基础_
查看>>
裁员浪潮,互联网人该何去何从?
查看>>
Python Day 01
查看>>
Android5.0之CoordinatorLayout的使用
查看>>
U盘安装Ubuntu14.4时遇到分区问题记录
查看>>
servlet工作原理解析
查看>>