android 简单的aidl

1.首先在src目录下建立一个file,命名为IPerson.aidl

复制代码
package com.example.aidldemo;

interface IPerson {
    void setAge(int age);
    void setName(String name);
    String display();
}
复制代码

2.接着要实现这个aidl里面的方法

复制代码
package com.example.aidldemo;

import android.os.RemoteException;

public class IPersonImpl extends IPerson.Stub{
    //声明两个变量
    private int age;
    private String name;
    @Override
    public void setAge(int age) throws RemoteException {
        this.age=age;
    }

    @Override
    public void setName(String name) throws RemoteException {
        this.name=name;
    }

    @Override
    public String display() throws RemoteException {
        return "name="+name+";age="+age;
    }

}
复制代码

3.建立一个service

复制代码
package com.example.aidldemo;

import com.example.aidldemo.IPerson.Stub;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyRemoteService extends Service {
    private Stub iPerson=new IPersonImpl();
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return iPerson;
    }

}
复制代码

4.在activity中启动服务

复制代码
package com.example.aidldemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        // 设置Intent Action 属性
        intent.setAction("com.example.aidldemo.action.MY_REMOTE_SERVICE");
        // 绑定服务
        startService(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
复制代码

5.新建一个项目,把上一个项目中的aidl拷贝到项目中
在activity中实现如下:

 

复制代码
package com.example.aidlclient;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.aidldemo.IPerson;

public class MainActivity extends Activity {
    private IPerson iPerson;
    private Button btn;
    // 实例化ServiceConnection
        private ServiceConnection conn = new ServiceConnection() {
            @Override
            synchronized public void onServiceConnected(ComponentName name, IBinder service) {
                // 获得IPerson接口
                iPerson = IPerson.Stub.asInterface(service);
                System.out.println("iperson----------:"+iPerson);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                iPerson=null;
            }
        };

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 设置当前视图布局
            setContentView(R.layout.activity_main);
            // 实例化Button
            btn = (Button) findViewById(R.id.button1);
            //为Button添加单击事件监听器
            
            // 实例化Intent
            Intent intent = new Intent("com.example.aidldemo.action.MY_REMOTE_SERVICE");
            // 设置Intent Action 属性
            bindService(intent, conn, Service.BIND_AUTO_CREATE);
            
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                    
                    try{
                        iPerson.setAge(20);
                        iPerson.setName("南瓜饼");
                        String msg = iPerson.display();
                        // 显示方法调用返回值
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            
        }
        
        @Override
        protected void onDestroy()
        {
            // TODO Auto-generated method stub
            unbindService(conn);
            super.onDestroy();
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
复制代码

 项目截图:

csdn下载地址:http://download.csdn.net/detail/wenwei19861106/4879164

发布了7 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u012913972/article/details/38326505