安卓:service,AID通信,有服务端和客户端的
来源:互联网 发布:linux里sort怎么用 编辑:程序博客网 时间:2024/06/02 05:51
如图:
服务端APP:清单文件里注册服务
1.创建接口,写一个抽象方法
2.去工作环境中找到项目把接口的后缀名改为.aidl
3.创建继承service的类,写一个返回字符串的方法
4.写一个内部类去继承接口名.Stub,里面实现的抽象方法返回的是步骤3写的方法
5.onBind返回的是步骤4创建的内部类
接口文件,service类文件在在同一包内即可
逻辑代码文件和布局文件不需改动
接口文件:
<span style="font-size:18px;">package com.example.day23_aidservice3;interface MyInterface {String aaa();}</span>
service类文件:
<span style="font-size:18px;">package com.example.day23_aidservice3;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service{@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}class MyBinder extends MyInterface.Stub{@Overridepublic String aaa(){return name();}}public String name(){return "李易峰";}}</span>
客户端APP:
1.创建一个和服务端接口所在包包名一样的包,在该包下创建接口(跟服务端的一样)
2.去工作环境中找到项目把接口后缀名改为.aidl
3.逻辑代码文件中定义一个接口类型变量
4.创建一个类实现ServiceConnection,里面onServiceConnected方法中写: myinterface=MyInterface.Stub.asInterface(service);
5.onCreate方法中创建Intent,绑定服务
6.按钮点击事件中直接调用接口方法
接口文件和服务端的一样,这里就不写了
逻辑代码文件:
<span style="font-size:18px;">package com.example.day23_aidclient3;import com.example.day23_aidservice3.MyInterface;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {MyInterface myinterface;//该文件所在包名需与服务端对应的所在包名一样 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=new Intent("mm.ss"); bindService(intent, new MyConn(), Context.BIND_AUTO_CREATE); } public void click(View v) { try {String name=myinterface.aaa();Toast.makeText(getApplicationContext(), name, 0).show();} catch (RemoteException e) {e.printStackTrace();} } class MyConn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//通过aidl获取接口对象myinterface=MyInterface.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {} }}</span>
布局文件:
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用服务里方法返回的数据" android:onClick="click"/></RelativeLayout></span>
0 0
- 安卓:service,AID通信,有服务端和客户端的
- 安卓:service间通信AID实现
- 安卓:service通信,信使实现,客户端和服务端及回传消息的完整版
- 安卓手机socket通信(服务端和客户端)
- 安卓客户端和服务端的交互自我解惑
- web Service的服务端和客户端
- 怎样实行客户端和服务端的通信
- TCP的客户端和服务端通信
- 简单的Socket通信客户端和服务端
- 客户端和服务端通信的简单小案列
- 基于服务端和客户端之间的通信
- Netty实现客户端和服务端的通信
- 安卓客户端,服务端
- 基于servlet的服务端与安卓客户端的通信(文本)
- Socket 客户端和服务端无限次通信,多个客户端和服务端通信的例子
- Socket服务端和客户端通信
- socket服务端和客户端通信
- 安卓客户端和服务器端的通信(勘误填坑版)
- 简单理解Socket
- srm 547
- GET和POST有什么区别?
- 【Unity】UGUI中RectTransform中Top和Bottom的设置,宽和高的设置,以及postion和锚点的设置
- EM算法简单理解
- 安卓:service,AID通信,有服务端和客户端的
- C++进阶学习——单例模式的实现
- 黑马程序员----C 语言学习笔记之结构体数组,指针,嵌套的使用
- Android系统栏相关(SystemBar)
- 证明方法------杂谈
- DoTween 一:< SetEase —— 渐变曲线 >
- iOS开发工具——网络封包分析工具Charles
- android系统通讯录源码--显示联系人添加长按事件监听
- 黑马程序员——C语言基础---结构体2