Android-在Android studio中实现远程服务(Service)AIDL
来源:互联网 发布:手机能不能做淘宝客服 编辑:程序博客网 时间:2024/06/05 19:20
在Android studio 实现远程服务(Service)AIDL的方法与在eclipse中实现有些不同,Android studio中自带了AIDL创建的方式,而eclipse中需要手动创建;
下面简单介绍实现远程服务(Service)AIDL的方法;
一、 创建服务工程应用项目 RemoteDemo
2. 创建 RemoteService 服务类
3. 创建AIDL文件, PublicBusiness 接口类
在创建之前在目录 build-->generated-->source-->aidl-->androidTest-->debug下面发现还没有任何文件
创建 PublicBusiness AIDL
创建后会出现如下 : 左边的main目录下出现aidl文件夹和PublicBusiness.aidl文件和包,将PublicBusiness.aidl文件中的basicTypes 方法删除
4. 在 PublicBusiness接口类中写入提供远程调用的方法,在确保PublicBusiness.aidl所在的包名与项目中默认的包名一致,如果一致,点击 Build-->Make Project(也可以直接点下图箭头指向的地方),生成相应的java文件。如果不一致,则改aidl的包名,改成一致,再点击生成,生成效果如图。
此时会发现在目录 build-->generated-->source-->aidl-->androidTest-->debug下面出现了编译的文件;
二、 创建使用远程服务中的工程应用项目 UseRemoteDemo工程:
1. 将 RemoteDemo中main目录下的aidl文件夹全部复制到 UseRemoteDemo工程 的main目录下即可;
三、下面是代码:
服务工程应用项目 RemoteDemo 代码:
注册服务:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><service android:name=".RemoteService" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.example.RemoteService"/> </intent-filter> </service></span>
MainActivity.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.remotedemo;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}</span>
RemoteService.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.remotedemo;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class RemoteService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("调用了bind方法"); return new XiaoMi(); } @Override public boolean onUnbind(Intent intent) { System.out.println("调用了onUnbind()方法"); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); System.out.println("调用了onCreate()方法"); } @Override public void onDestroy() { super.onDestroy(); System.out.println("调用了onDestroy()方法"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("调用了onStartCommand()方法"); return super.onStartCommand(intent, flags, startId); } //中间人对象 class XiaoMi extends PublicBusiness.Stub{ @Override public void qianXian() throws RemoteException { // 调用服务的banzheng方法 RemoteService.this.banzheng(); } } public void banzheng(){ System.out.println("李局帮你来办证"); }}</span>
PublicBusiness.aidl 接口类
<span style="font-family:KaiTi_GB2312;font-size:18px;">// PublicBusiness.aidlpackage com.example.yu_longji.remotedemo;// Declare any non-default types here with import statementsinterface PublicBusiness { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void qianXian();}</span>
使用远程服务中的工程应用项目 UseRemoteDemo工程
布局代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout 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:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click1" android:text="启动远程服务" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click2" android:text="停止远程服务" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click3" android:text="绑定远程服务" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click4" android:text="解绑远程服务" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click5" android:text="远程办证" /></LinearLayout></span>
MainActivity.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.useremotedemo;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.View;import com.example.yu_longji.remotedemo.PublicBusiness;public class MainActivity extends Activity { private MyserviceConn conn; PublicBusiness pb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); conn = new MyserviceConn(); } //绑定远程服务 public void click3(View view){ Intent intent = new Intent(); intent.setAction("com.example.RemoteService"); bindService(intent, conn, BIND_AUTO_CREATE); } //解绑远程服务 public void click4(View view){ Intent intent = new Intent(); intent.setAction("com.example.RemoteService"); unbindService(conn); } //实现ServiceConnection接口 public class MyserviceConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { // 使用aidl中自动生成的方法来强转 //把Ibinder中间人对象强转成publicbusiness pb = PublicBusiness.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } } //启动远程服务 public void click1(View view){ Intent intent = new Intent(); intent.setAction("com.example.RemoteService"); startService(intent); } //停止远程服务 public void click2(View view){ Intent intent = new Intent(); intent.setAction("com.example.RemoteService"); stopService(intent); } //远程办证 public void click5(View view){ //调用远程服务的qianXian方法 try { pb.qianXian(); } catch (RemoteException e) { e.printStackTrace(); } }}</span>
从RemoteDemo项目中复制的PublicBusiness.aidl文件内容不变;
运行后的结果:
- Android-在Android studio中实现远程服务(Service)AIDL
- Android-在Android studio中实现远程服务(Service)AIDL
- 在Android Studio中实现AIDL远程服务调用
- Android studio 使用aidl实现远程服务
- Android studio Service和aidl远程服务笔记
- Android中的服务(service)--AIDL远程服务
- Android Studio实现Service AIDL
- Android Studio实现Service AIDL
- Android studio实现Service AIDL
- Android AIDL实现远程服务
- Android AIDL 使用 2 --在android studio中实现AIDL
- Aidl在android studio中实现
- Android:远程服务Service(含AIDL & IPC讲解)
- Android:远程服务Service(含AIDL & IPC讲解)
- Android中不使用AIDL实现Service的远程调用
- Android中不使用AIDL实现Service的远程调用
- Android AIDL 远程service
- Android服务Service详解(作用,生命周期,AIDL)系列文章--远程服务AIDL&IPC
- Android 4.4 沉浸式透明状态栏与导航栏,android4.4
- swift 错误处理
- Shiny应用基础(5):数据获取与响应
- C# WPF数据绑定
- 常用的mime-mapping配置
- Android-在Android studio中实现远程服务(Service)AIDL
- 树的镜像
- 代码实现List下载列表(安卓自定义控件,List列表)
- C# 窗体程序 DataGridView 输出换行
- BZOJ 题目3172: [Tjoi2013]单词(AC自动机||AC自动机+fail树||后缀数组暴力||后缀数组+RMQ+二分等五种姿势水过)
- Xamarin.Forms 之简单介绍
- 进入MAC下面的Library目录
- PS中,如何给文字填充成渐变色
- ubuntu环境下tftp安装