android 简单的aidl
来源:互联网 发布:婚纱视频制作软件 编辑:程序博客网 时间:2024/05/21 21:35
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
- android 简单的aidl
- android 简单的aidl
- Android AIDL的简单例子
- android的AIDL简单介绍
- android AIDL的简单使用
- Android AIDL的简单应用
- Android AIDL的简单使用
- Android中 AIDL 的简单实用例子
- 最简单的Android Aidl 使用
- 创建一个简单的android aidl工程
- android中aidl的简单使用
- android AIDL技术的简单演示
- android简单的AIDL使用示例
- android AIDL 入门,最最简单的例子
- 一个android中AIDL的简单例子
- android AIDL简单步骤
- Android Aidl简单实例
- Android AIDL简单例子
- android 第一次运行应用的引导界面
- java线程同步
- android 大图片在listview中内存溢出的问题
- Android handler异步更新
- android 代码片段
- android 简单的aidl
- android BroadcastReceiver
- android 最全的shape属性
- android pcm编解码
- [Java]读取文件方法大全
- android jni操作数组
- android jni打印log
- [object-c基础]delegate机制
- C语言memcpy和strncat