安卓:service间通信AID实现
来源:互联网 发布:鹿晗 知乎 编辑:程序博客网 时间:2024/06/06 17:55
一。
如图:
清单文件里注册服务
1.创建一个接口,写一个抽象方法
2.创建一个继承Service的类,写一个返回图中所示名的方法
3.再写一个内部类继承Binder并实现步骤1建的接口,实现的抽象方法里返回步骤2中写的方法
4.onBind方法里返回的是new 内部类名();
5.主逻辑代码文件中,创建一个内部类实现ServiceConnection接口
6. 定义一个接口类型变量,onServiceConnected方法里写:myinterface=(MyInterface) service;
7.onCreate方法里写Intent,然后绑定服务: bindService(intent, conn, Context.BIND_AUTO_CREATE);
8.按钮事件里直接调用接口里方法
逻辑代码:
<span style="font-size:18px;">package com.example.day23_aidservice;import com.example.day23_aidservice.MyService.MyBinder;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.view.View;import android.widget.Toast;public class MainActivity extends Activity {MyBinder binder;MyInterface mi;MyConn conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=new Intent(); intent.setAction("aa.bb.cc"); conn=new MyConn(); bindService(intent, conn, Context.BIND_AUTO_CREATE); } public void click(View v) {//String name1=binder.aaa();String name1=mi.aaa();Toast.makeText(getApplicationContext(), name1, 0).show();System.out.println("===>"+name1); } class MyConn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//binder=(MyBinder) service;mi=(MyInterface) 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:onClick="click" android:text="接口调服务里方法" /></RelativeLayout></span>
接口:
<span style="font-size:18px;">package com.example.day23_aidservice;public interface MyInterface {public String aaa();}</span>
service类:
<span style="font-size:18px;">package com.example.day23_aidservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class MyService extends Service{@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}public class MyBinder extends Binder implements MyInterface{@Overridepublic String aaa() {return name();}}public String name(){return "zhaolinger";}}</span>
二。
效果跟一一样,代码也差不多,改动几处
1.创建一个接口,写一个抽象方法
2.去工作环境找到项目,然后把接口的后缀名改为aidl
3..创建一个继承Service的类,写一个返回图中所示名的方法
4.再写一个内部类继承步骤1建的接口名.Stub,实现的抽象方法里返回步骤2中写的方法
5.onBind方法里返回的是new 内部类名();
6.主逻辑代码文件中,创建一个内部类实现ServiceConnection接口
7. 定义一个接口类型变量,onServiceConnected方法里写: myinterface=MyInterface.Stub.asInterface(service);
8.onCreate方法里写Intent,然后绑定服务: bindService(intent, conn, Context.BIND_AUTO_CREATE);
接口文件(写完再改后缀名):
<span style="font-size:18px;">package com.example.day23_aidservice2;interface MyInterface {String aaa();}</span>
逻辑代码文件:
<span style="font-size:18px;">package com.example.day23_aidservice2;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 {MyConn conn;MyInterface minte; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=new Intent("hh.pp"); conn=new MyConn(); bindService(intent, conn, Context.BIND_AUTO_CREATE); } public void click(View v) {try { String name= minte.aaa(); Toast.makeText(getApplicationContext(), name, 0).show();} catch (RemoteException e){e.printStackTrace();} } class MyConn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {minte=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="AID" android:onClick="click"/></RelativeLayout></span>
服务类文件:
<span style="font-size:18px;">package com.example.day23_aidservice2;import android.app.Service;import android.content.Intent;import android.os.Binder;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 "shanxuyang";}}</span>
- 安卓:service间通信AID实现
- 安卓:service,AID通信,有服务端和客户端的
- 安卓:service通信,信使实现,客户端和服务端及回传消息的完整版
- 安卓Service组件使用系列6:使用AIDL完成两个进程间的通信
- 安卓绑定本地服务Service并与之通信
- 安卓学习 AIDL跨应用Service绑定与通信
- 安卓Web Service实现天气预报功能
- 安卓实现组件间通信的几种方法
- Service+AIDL牛刀小试,实现应用间通信
- 安卓进程间通信
- 安卓多线程间通信和多进程之间通信有什么不同?分别怎么实现?
- 安卓1109_四大组件-Service 电话监听 bindservice 接口调用 线程间的通信 对话框
- 安卓PC实现通信与UDP的Java实现
- 安卓 Service组件
- 关于安卓service
- 安卓Service服务
- 安卓016Service
- 安卓service 基础
- 多态的实现
- zigbee学习之中断led
- Web测试学习资源和主要测试点
- HDU 2031 十进制转换到任意进制
- ios无限旋转
- 安卓:service间通信AID实现
- C#根据路径获取子文件夹和文件
- Flume NG 简介及配置实战
- 一些经常用到的linux命令
- 打开CMDLINE中的 ” earlyprink “ 参数
- 【Ubuntu下配置shadowsocks-libv客户端】
- 局部对象
- Windows下图文详解PHP三种运行方式(php_mod、cgi、fastcgi)
- Longest Substring Without Repeating Characters