安卓:service间通信AID实现
2015-09-29 16:08
513 查看
一。
如图:
清单文件里注册服务
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.按钮事件里直接调用接口里方法
逻辑代码:
布局文件:
接口:
service类:
二。
效果跟一一样,代码也差不多,改动几处
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);
9.按钮事件里直接调用接口里方法
接口文件(写完再改后缀名):
逻辑代码文件:
布局文件:
服务类文件:
如图:
清单文件里注册服务
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 { @Override public void onServiceConnected(ComponentName name, IBinder service) { //binder=(MyBinder) service; mi=(MyInterface) service; } @Override public 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{ @Override public IBinder onBind(Intent intent) { return new MyBinder(); } public class MyBinder extends Binder implements MyInterface { @Override public 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);
9.按钮事件里直接调用接口里方法
接口文件(写完再改后缀名):
<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 { @Override public void onServiceConnected(ComponentName name, IBinder service) { minte=MyInterface.Stub.asInterface(service); } @Override public 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{ @Override public IBinder onBind(Intent intent) { return new MyBinder(); } class MyBinder extends MyInterface.Stub { @Override public String aaa() { return name(); } } public String name() { return "shanxuyang"; } } </span>
相关文章推荐
- Windows Server 2012 磁盘管理之 简单卷、跨区卷、带区卷、镜像卷和RAID-5卷
- LeetCode -- Climbing Stairs
- Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP
- chrome浏览器调试报错:Failed to load resource: the server responsed width a status of 404 (Not Found)…http://127.0.0.1:5099/favicon.ico
- failed to install service or service already installed
- tair(四)--命令
- git clone error: RPC failed; result=56, HTTP code = 200 错误解决
- tair(三)--安装配置
- exits和In的区别(http://hi.baidu.com/delphi_relive/blog/item/d7c0034a49c4932208f7ef21.html)
- tair(二)--概述
- tair(一)--入门
- 增加samba用户提示Failed to add entry for user
- editor does not contain a main type的解决方案
- 文件系统检测工具AIDE
- how to prevent hadoop job to fail on corrupted input file
- 采用opencv_cascadetrain进行训练的步骤及注意事项
- Win10 用户强烈要求:取消登录界面Email信息,避免暴露隐私
- 用opencv的traincascade.exe训练行人的HAAR、LBP和HOG特征的xml
- Odoo 8.0 新API 之constrains装饰
- libgdx API之AI:AI让游戏对象思考