Android-在Android studio中实现远程服务(Service)AIDL

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 代码:

注册服务:

[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><service  
  2.             android:name=".RemoteService"  
  3.             android:enabled="true"  
  4.             android:exported="true" >  
  5.             <intent-filter>  
  6.                 <action android:name="com.example.RemoteService"/>  
  7.             </intent-filter>  
  8.         </service></span>  


MainActivity.java

[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.remotedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class MainActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13.   
  14. }  
  15. </span>  

RemoteService.java
[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.remotedemo;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6. import android.os.RemoteException;  
  7.   
  8. public class RemoteService extends Service {  
  9.   
  10.     @Override  
  11.     public IBinder onBind(Intent intent) {  
  12.         System.out.println("调用了bind方法");  
  13.         return new XiaoMi();  
  14.     }  
  15.   
  16.     @Override  
  17.     public boolean onUnbind(Intent intent) {  
  18.         System.out.println("调用了onUnbind()方法");  
  19.         return super.onUnbind(intent);  
  20.     }  
  21.   
  22.     @Override  
  23.     public void onCreate() {  
  24.         super.onCreate();  
  25.         System.out.println("调用了onCreate()方法");  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onDestroy() {  
  30.         super.onDestroy();  
  31.         System.out.println("调用了onDestroy()方法");  
  32.     }  
  33.   
  34.     @Override  
  35.     public int onStartCommand(Intent intent, int flags, int startId) {  
  36.         System.out.println("调用了onStartCommand()方法");  
  37.         return super.onStartCommand(intent, flags, startId);  
  38.     }  
  39.     //中间人对象  
  40.     class XiaoMi extends PublicBusiness.Stub{  
  41.   
  42.         @Override  
  43.         public void qianXian() throws RemoteException {  
  44.             //  调用服务的banzheng方法  
  45.             RemoteService.this.banzheng();  
  46.         }  
  47.     }  
  48.   
  49.     public void banzheng(){  
  50.         System.out.println("李局帮你来办证");  
  51.     }  
  52. }  
  53. </span>  

PublicBusiness.aidl 接口类
[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">// PublicBusiness.aidl  
  2. package com.example.yu_longji.remotedemo;  
  3.   
  4. // Declare any non-default types here with import statements  
  5.   
  6. interface PublicBusiness {  
  7.     /** 
  8.      * Demonstrates some basic types that you can use as parameters 
  9.      * and return values in AIDL. 
  10.      */  
  11.     void qianXian();  
  12. }  
  13. </span>  


使用远程服务中的工程应用项目     UseRemoteDemo工程

布局代码:

[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <Button  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:onClick="click1"  
  13.         android:text="启动远程服务" />  
  14.   
  15.     <Button  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:onClick="click2"  
  19.         android:text="停止远程服务" />  
  20.   
  21.     <Button  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:onClick="click3"  
  25.         android:text="绑定远程服务" />  
  26.   
  27.     <Button  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:onClick="click4"  
  31.         android:text="解绑远程服务" />  
  32.   
  33.     <Button  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:onClick="click5"  
  37.         android:text="远程办证" />  
  38.   
  39. </LinearLayout>  
  40. </span>  

MainActivity.java
[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.useremotedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.Bundle;  
  8. import android.os.IBinder;  
  9. import android.os.RemoteException;  
  10. import android.view.View;  
  11.   
  12. import com.example.yu_longji.remotedemo.PublicBusiness;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private MyserviceConn conn;  
  17.     PublicBusiness pb;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.         conn = new MyserviceConn();  
  25.     }  
  26.   
  27.     //绑定远程服务  
  28.     public void click3(View view){  
  29.         Intent intent = new Intent();  
  30.         intent.setAction("com.example.RemoteService");  
  31.         bindService(intent, conn, BIND_AUTO_CREATE);  
  32.   
  33.     }  
  34.     //解绑远程服务  
  35.     public void click4(View view){  
  36.         Intent intent = new Intent();  
  37.         intent.setAction("com.example.RemoteService");  
  38.         unbindService(conn);  
  39.     }  
  40.   
  41.         //实现ServiceConnection接口  
  42.     public class MyserviceConn implements ServiceConnection {  
  43.         @Override  
  44.         public void onServiceConnected(ComponentName name, IBinder service) {  
  45.             // 使用aidl中自动生成的方法来强转  
  46.             //把Ibinder中间人对象强转成publicbusiness  
  47.             pb = PublicBusiness.Stub.asInterface(service);  
  48.         }  
  49.         @Override  
  50.         public void onServiceDisconnected(ComponentName name) {  
  51.   
  52.         }  
  53.     }  
  54.   
  55.     //启动远程服务  
  56.     public void click1(View view){  
  57.         Intent intent = new Intent();  
  58.         intent.setAction("com.example.RemoteService");  
  59.         startService(intent);  
  60.     }  
  61.     //停止远程服务  
  62.     public void click2(View view){  
  63.         Intent intent = new Intent();  
  64.         intent.setAction("com.example.RemoteService");  
  65.         stopService(intent);  
  66.     }  
  67.   
  68.     //远程办证  
  69.     public void click5(View view){  
  70.   
  71.         //调用远程服务的qianXian方法  
  72.         try {  
  73.             pb.qianXian();  
  74.         } catch (RemoteException e) {  
  75.             e.printStackTrace();  
  76.         }  
  77.     }  
  78.   
  79. }  
  80. </span>  

从RemoteDemo项目中复制的PublicBusiness.aidl文件内容不变;

运行后的结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值