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

移动开发qq群:59516399

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值