aidl的简单快速入门

来源:互联网 发布:网络黑白电子版网盘 编辑:程序博客网 时间:2024/06/01 07:40

原文链接  http://blog.csdn.net/double2hao/article/details/51626347  侵删


为何要开启多进程?主要有两种情况:

一、一个应用由于自身需要采用多进程模式来实现。比如播放器之类,如果仅仅在service中运行会影响主线程的响应速度,很可能会造成ANR,一般情况下不会这么写;如果仅仅在子线程中运行,一旦开启该线程的Activity被杀死后,线程也被杀死,无法实现后台运行效果,更加不合理。而如果在另外一个进程中使用service后台运行,就显得十分恰当了。

二、由于android对单个应用所使用的最大内存做了限制,为了加大一个应用可使用的内存,所以通过多进程来获取多份内存空间。


本篇文章demo重点:(demo源码在文章结尾)

1、开启多进程

2、两个进程之间使用AIDL进行通信


开启多进程:

在Android中常用的使用多进程只有一种办法,那就是在AndroidManifest中为四大组件(Activity、Service、Broadcast Receiver、ContentProvier)指定android:process属性。笔者demo中的远程service如下图:



最终绑定该service后在DDMS中进程的显示情况如下图:



可以看到最后的两个进程都是同一个包名,只是第二个是“:remote”。这样就非常简单的开启了多进程。


讲到此处,很多好奇的读者定然有疑问了,“android:process”中的参数到底代表了什么?简单来讲就是代表了新开的这个进程的id。如果两个应用要共享同一个进程就需要用到这个了。

那么笔者此处写的“:remote”又是什么意思呢?“remote”不是关键,这个完全可以自己随意取名字,“:”冒号才是关键

进程名以“:”开头的进程属于当前应用的私有进程,其他应用的组件不可以和它跑在同一个进程中。而进程名不以“:”开头的进程属于全局进程,其他应用可以通过某些方式和它跑在同一个进程中。


两个进程之间使用AIDL进行通信:

笔者此篇文章实现的主要效果:

能够在当前进程中MainActivity,运行另一个进程中开启的Service中实现的方法testMethod(),方法与最终效果如下:




界面上主要有两个按钮,第一个是开启远程进程中的Service,另一个为执行该方法。



主要实现步骤:(主要有三条,分别为AIDL、Service、和调用处(demo中为MainActivity))

1、创建一个AIDL接口,并写入自己要在进程间通信用的抽象方法。

myAIDL.aidl:

[java] view plain copy
  1. package com.example.double2.myaidltest;  
  2.   
  3. interface myAIDL {  
  4.   
  5.     void testMethod();  
  6. }  

创建AIDL文件与创建java文件等类似,直接右击创建即可。android studio中就十分方便,会自动在main文件下创建一个aidl文件夹,并在该文件夹创建于你项目名相同的包名。

可能遇到的小问题:

笔者第一次创建AIDL,在Service中发现找不到该AIDL的包。遇到相同问题的读者可以在创建AIDL并写完抽象方法之后使用build->make project重新构建一下项目。


2、创建一个远程Service,在Service中创建一个类继承AIDL接口中的Stub类并实现Stub中的抽象方法,最后不要忘记在onBind中返回这个类的对象。

[java] view plain copy
  1. public class AIDLRemoteService extends Service {  
  2.     private static final String TAG = "AIDLRemoteService";  
  3.   
  4.     private final myAIDL.Stub mBinder=new myAIDL.Stub(){  
  5.         @Override  
  6.         public void testMethod() throws RemoteException {  
  7.             Log.d(TAG, "testMethod: "+"this is myAIDLTest");  
  8.         }  
  9.     };  
  10.   
  11.     @Override  
  12.     public IBinder onBind(Intent intent) {  
  13.         return mBinder;  
  14.     }  
  15. }  


3、在要调用的地方(笔者demo中就为MainActivity中)绑定该Service,将Service返回的Binder对象转换成AIDL接口所属的类型,接着直接调用AIDL的方法。

在成功连接之后,将Service返回的Binder对象转换成AIDL接口所属的类型:

[java] view plain copy
  1. private myAIDL mMyAIDL;  
  2.     private ServiceConnection mServiceConnection = new ServiceConnection() {  
  3.         @Override  
  4.         public void onServiceConnected(ComponentName name, IBinder service) {  
  5.             Log.e(TAG, "onServiceConnected");  
  6.             mMyAIDL = myAIDL.Stub.asInterface(service);  
  7.         }  
  8.   
  9.         @Override  
  10.         public void onServiceDisconnected(ComponentName name) {  
  11.             Log.e(TAG, "onServiceDisconnected");  
  12.             mMyAIDL = null;  
  13.         }  
  14.     };  

在调用处直接使用:

[java] view plain copy
  1. btnStartMethod.setOnClickListener(new View.OnClickListener() {  
  2.             @Override  
  3.             public void onClick(View v) {  
  4.   
  5.                 try {  
  6.                     mMyAIDL.testMethod();  
  7.                 } catch (RemoteException e) {  
  8.                     Toast.makeText(MainActivity.this"服务被异常杀死,请重新开启。", Toast.LENGTH_SHORT).show();  
  9.                 }  
  10.   
  11.             }  
  12.         });  


demo项目结构:



myAIDL.aidl:

[java] view plain copy
  1. package com.example.double2.myaidltest;  
  2.   
  3. interface myAIDL {  
  4.   
  5.     void testMethod();  
  6. }  


AIDLRemoteService:

[java] view plain copy
  1. package com.example.double2.myaidltest;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6. import android.os.RemoteException;  
  7. import android.util.Log;  
  8.   
  9. /** 
  10.  * 项目名称:MyAIDLTest 
  11.  * 创建人:Double2号 
  12.  * 创建时间:2016/6/10 8:13 
  13.  * 修改备注: 
  14.  */  
  15. public class AIDLRemoteService extends Service {  
  16.     private static final String TAG = "AIDLRemoteService";  
  17.   
  18.     private final myAIDL.Stub mBinder=new myAIDL.Stub(){  
  19.         @Override  
  20.         public void testMethod() throws RemoteException {  
  21.             Log.d(TAG, "testMethod: "+"this is myAIDLTest");  
  22.         }  
  23.     };  
  24.   
  25.     @Override  
  26.     public IBinder onBind(Intent intent) {  
  27.         return mBinder;  
  28.     }  
  29. }  


MainActivity:

[java] view plain copy
  1. package com.example.double2.myaidltest;  
  2.   
  3. import android.content.ComponentName;  
  4. import android.content.Context;  
  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.support.v7.app.AppCompatActivity;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends AppCompatActivity {  
  17.   
  18.     private static final String TAG = "MainActivity";  
  19.     private Button btnBindService;  
  20.     private Button btnStartMethod;  
  21.   
  22.     private myAIDL mMyAIDL;  
  23.     private ServiceConnection mServiceConnection = new ServiceConnection() {  
  24.         @Override  
  25.         public void onServiceConnected(ComponentName name, IBinder service) {  
  26.             Log.e(TAG, "onServiceConnected");  
  27.             mMyAIDL = myAIDL.Stub.asInterface(service);  
  28.         }  
  29.   
  30.         @Override  
  31.         public void onServiceDisconnected(ComponentName name) {  
  32.             Log.e(TAG, "onServiceDisconnected");  
  33.             mMyAIDL = null;  
  34.         }  
  35.     };  
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.activity_main);  
  41.   
  42.         initView();  
  43.     }  
  44.   
  45.     private void initView() {  
  46.         btnBindService = (Button) findViewById(R.id.btn_bind_service);  
  47.         btnStartMethod = (Button) findViewById(R.id.btn_start_method);  
  48.         btnBindService.setOnClickListener(new View.OnClickListener() {  
  49.             @Override  
  50.             public void onClick(View v) {  
  51.                 Intent intent = new Intent(MainActivity.this, AIDLRemoteService.class);  
  52.                 bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);  
  53.             }  
  54.         });  
  55.   
  56.         btnStartMethod.setOnClickListener(new View.OnClickListener() {  
  57.             @Override  
  58.             public void onClick(View v) {  
  59.   
  60.                 try {  
  61.                     mMyAIDL.testMethod();  
  62.                 } catch (RemoteException e) {  
  63.                     Toast.makeText(MainActivity.this"服务被异常杀死,请重新开启。", Toast.LENGTH_SHORT).show();  
  64.                 }  
  65.   
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     @Override  
  71.     protected void onDestroy() {  
  72.         super.onDestroy();  
  73.         unbindService(mServiceConnection);  
  74.     }  
  75. }  

activity_main:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     >  
  8.   
  9.     <Button  
  10.         android:id="@+id/btn_bind_service"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:textSize="20sp"  
  14.         android:text="bindService"/>  
  15.   
  16.     <Button  
  17.         android:id="@+id/btn_start_method"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:textSize="20sp"  
  21.         android:text="startMethod"/>  
  22.   
  23. </LinearLayout>  

AndroidManifest:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.example.double2.myaidltest">  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:supportsRtl="true"  
  10.         android:theme="@style/AppTheme">  
  11.         <activity android:name=".MainActivity">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN"/>  
  14.   
  15.                 <category android:name="android.intent.category.LAUNCHER"/>  
  16.             </intent-filter>  
  17.         </activity>  
  18.   
  19.         <service android:name=".AIDLRemoteService"  
  20.             android:process=":remote"/>  
  21.     </application>  
  22.   
  23. </manifest>  


demo源码地址:http://download.csdn.net/detail/double2hao/9545551


原创粉丝点击