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

    新建一个项目充当发送者

    1.创建一个以.aidl为后缀名的文件

    IMyService.aidl

    package com.example.aidldemo

    interface IMyService{

      String getValue();

      String getName();

      String getPrice();

    }

    系统会在gen目录中自动创建一个IMyService类

    2.编写一个服务 并在服务中实现IMyService接口

    private IMyservice.Stub binder = new IMyservice.Stub() {      

       public String getValue() throws RemoteException {    

        // TODO Auto-generated method stub    

        return "Android";   

        }

        public String getName() throws RemoteException {    

        // TODO Auto-generated method stub    

        return "hello world";   

        }

        public float getPrice() throws RemoteException {   

         // TODO Auto-generated method stub    

        return 10.0F;   

      }  

    };

    在manifase中注册MyService添加android:process=":remote"

    由于需要在其他应用中使用,所以要在remote之前要添加冒号

    给service添加过滤器

    <intent-filter >
                    <action android:name="com.example.aidldemo.IMyservice"/>

    </intent-filter>

    另外建一个项目充当接受者

    把上面的.aidl文件复制到这个项目中,报名要相同

    在布局文件中放一个textview用来接受value

    放三个button,第一个用来bind服务  第二个用来显示向textview赋值  第三个解绑

    在mainavtivity类

    package com.example.aidlkhd;
    
    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.os.RemoteException;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.example.aidldemo.IMyservice;
    
    public class MainActivity extends Activity {
    	private Button button1,button2,button3;
    	private TextView textview;
    	private IMyservice iservice = null;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		button1 = (Button) findViewById(R.id.button1);
    		button2 = (Button) findViewById(R.id.button2);
    		button3 = (Button) findViewById(R.id.button3);
    		textview = (TextView) findViewById(R.id.textview);
    	}
    	public void bind(View v){
    		Intent service = new Intent(IMyservice.class.getName());
    		bindService(service,connection , BIND_AUTO_CREATE);
    	}
    	public void getdata(View v){
    		try {
    			textview.setText("运行结果="+iservice.getValue());
    		} catch (RemoteException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	public void unbind(View v){
    		unbindService(connection);
    	}
    	private ServiceConnection connection = new ServiceConnection(){
    
    		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
    			// TODO Auto-generated method stub
    			//从远程service中获得AIDL实例化对象
    			iservice = IMyservice.Stub.asInterface(arg1);
    			System.out.println("Bind Success:"+iservice);
    		}
    
    		public void onServiceDisconnected(ComponentName arg0) {
    			// TODO Auto-generated method stub
    			iservice = null;
    		}
    		
    	};
    	@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;
    	}
    
    }
    
  • 相关阅读:
    单例设计模式
    网络编程--Socket与ServerSocket
    JDBC连接Oracle数据库
    ObjectInputStream与ObjectOutputStream
    MyBatis的SQL语句映射文件详解(二)----增删改查
    MyBatis的SQL语句映射文件详解
    MyBatis+Spring实现基本CRUD操作
    通俗解释IOC原理
    Git菜鸟
    hibernate连接oracle数据库
  • 原文地址:https://www.cnblogs.com/dcxz/p/3342549.html
Copyright © 2011-2022 走看看