zoukankan      html  css  js  c++  java
  • 进程间通讯aidl

    进程间通讯(aidl)
    1.首先定义一个接口
    2.把这个接口的文件扩展名改为xxx.aidl
    3.写一个MyService类继承自Service类重新里面的方法,
    4.在MyService类定义一个内部类继承自Stub类
    5.在onBind()方法把内部类的一个实例作为返回值同外部访问
    6.在MainActivity中通过bindService(intent,ServiceConnection,flag)方法中的第二个参数他是一个接口,要一个类继承或者通过匿名对象作为参数
      它里面有两个方法onServiceDisconnected(ComponentName name)和onServiceConnected(ComponentName name, IBinder service)在第二onServiceConnected方法中
      通过Stub.asInterface(service);得到一个接口的实例,那么就可以调用接口里面的方法了
     
     如果是在当前应用访问其他应用只需要把aidl文件赋值到当前项目即可,但必须连包一起复制,访问步骤和上面的一样
    
    interface Person {
    	String getName();
    }
    
    
    public class MyService extends Service {
    
    	@Override
    	public IBinder onBind(Intent intent) {
    		return new MyBinder();
    	}
    	
    	class MyBinder extends Stub{
    		public String getName() throws RemoteException {
    			return "活动结束了";
    		}
    	}
    
    }
    
    
    
    public class MainActivity extends Activity {
    
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    				
    		Intent service=new Intent(this,MyService.class);
    		bindService(service, new ServiceConnection() {
    			public void onServiceDisconnected(ComponentName name) {	}
    			
    			public void onServiceConnected(ComponentName name, IBinder service) {
    				Person person = Stub.asInterface(service);
    				try {
    					System.out.println(person.getName());
    				} catch (RemoteException e) {
    					e.printStackTrace();
    				}
    			}
    		}, BIND_AUTO_CREATE);
    	
    	}
    
    }
    
  • 相关阅读:
    和大家一起了解Windows Server 2016 License许可证授权方式
    centos的cockpit安装使用
    Nscan-20150223
    JexBoss-20170328
    InsightScan-20150320
    Clusterd-20151119
    BBScan-20200419
    Astra-20190405
    为什么计算机计算时间要从1970年开始
    Appium环境搭建
  • 原文地址:https://www.cnblogs.com/tianshidechibang234/p/3886986.html
Copyright © 2011-2022 走看看