android绑定远程服务以及android接口定义语言(aidl)
来源:互联网 发布:淘宝怎么改差评为好评 编辑:程序博客网 时间:2024/04/29 14:21
package com.example.remote;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;import android.widget.Toast;public class Myservice extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("服务绑定");return new MiddlePerson();}@Overridepublic void onCreate() {// TODO Auto-generated method stubSystem.out.println("服务创建");super.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("服务结束");super.onDestroy();}public void methodInservice(String arr){System.out.println("我是远程服务方法,我被调用了");}private class MiddlePerson extends IMiddlePerson.Stub{@Overridepublic void call(String string) throws RemoteException {// TODO Auto-generated method stubmethodInservice(string);}}}编写aidl接口
- JAVA基本数据类型不需要导入
- String,List,Map和CharSequence不需要导入
package com.example.remote; interface IMiddlePerson {void call(String string);}
其次在清单文件中声明服务:
<service android:name="com.example.remote.Myservice"> <intent-filter ><action android:name="com.example.remote.Myservice"/></intent-filter> </service>
二:编写调用远程服务的程序:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="bind" android:text="绑定远程" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mystop" android:text="解除绑定" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:text="调用方法" /></LinearLayout>
调用实现:
package com.example.bindremote;import com.example.remote.IMiddlePerson;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.MenuItem;import android.view.View;public class MainActivity extends Activity {private IMiddlePerson im;private Myconn conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void mystop(View vew){unbindService(conn);} public void bind(View vew){ Intent service=new Intent(); service.setAction("com.example.remote.Myservice"); conn=new Myconn(); bindService(service, conn, BIND_AUTO_CREATE); } public void call(View vew){ try {im.call("leigewudi");System.out.println(im);} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();} }public class Myconn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubim=IMiddlePerson.Stub.asInterface(service);System.out.println("服务被调用了"+im);}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubunbindService(conn);System.out.println("服务结束");}}}
实验结果:
0 0
- android绑定远程服务以及android接口定义语言(aidl)
- AIDL android接口定义语言
- Android接口定义语言(AIDL)
- Android接口定义语言(AIDL)
- Android AIDL接口定义语言
- aidl(android 接口定义语言)
- Android 接口定义语言 (AIDL)
- Android 采用AIDL绑定远程服务
- android 的aidl绑定远程服务
- Android使用aidl来绑定远程服务
- Android -- Android 接口定义语言 (AIDL)
- android,aidl,绑定远程服务,调用远程服务的方法
- Android 接口定义语言(AIDL)
- Android的接口定义语言(aidl)
- 1、android散记-AIDL接口定义语言
- Android接口定义语言---AIDL(一)
- Android接口定义语言---AIDL(二)
- Android接口定义语言---AIDL(三)
- Dynamics CRM2016 New features in Microsoft Dynamics CRM Online 2015 Update 1 are now available
- xampp安装配置使用问题总结phpmail()函数发送邮件
- curl http调试restful接口
- Oracle中的left join、right join、inner join
- Shell教程(三):数组/Arrays、基本运算符
- android绑定远程服务以及android接口定义语言(aidl)
- SCI论文从入门到精通
- IOS:dispatch_group_enter的学习
- 卫星导航术语 -- 词汇与概念解释(200条)(转自今日北斗)
- Android blueZ HCI(一):hciconfig实现及常用方法
- URI,URN,URL三者的关系
- C++11 并发实战阅读笔记(2)
- 让apache支持CGI
- having与group by