标签:android des style blog http java
Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.第一句最重要,“只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL”,其他情况下你都可以选择其他方法,如使用Messager,也能跨进程通讯。可见AIDL是处理多线程、多客户端并发访问的。而Messager是单线程处理。还是官方文档说的明白,一句话就可以理解为什么要有AIDL。那么是不是这样的写个AIDL试试。
// IRemoteService.aidl package com.example.android; // Declare any non-default types here with import statements /** Example service interface */ interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ int getPid(); /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); }这段代码也是官方文档的。命名为IRemoteService.aidl,放在com.example.android包下(这个可以随意),保存后Android编译器会在gen目录下自动生成IRemoteService.java文件
package com.example.service; import com.example.android.IRemoteService; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Process; public class DDService extends Service { @Override public void onCreate() { super.onCreate(); System.out.println("DDService onCreate........" + "Thread: " + Thread.currentThread().getName()); } @Override public IBinder onBind(Intent arg0) { System.out.println("DDService onBind"); return mBinder; } private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ System.out.println("Thread: " + Thread.currentThread().getName()); System.out.println("DDService getPid "); return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { System.out.println("Thread: " + Thread.currentThread().getName()); System.out.println("basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString); } }; }
package com.example.aidlclient; 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.Process; import android.os.RemoteException; import android.view.View; import com.example.android.IRemoteService; public class MainActivity extends Activity { private IRemoteService remoteService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { remoteService = IRemoteService.Stub.asInterface(service); try { int pid = remoteService.getPid(); int currentPid = Process.myPid(); System.out.println("currentPID: " + currentPid +" remotePID: " + pid); remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明白"); } catch (RemoteException e) { e.printStackTrace(); } System.out.println("bind success! " + remoteService.toString()); } }; /** * 监听按钮点击 * @param view */ public void buttonClick(View view) { System.out.println("begin bindService"); Intent intent = new Intent("duanqing.test.aidl"); bindService(intent, conn, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); unbindService(conn); } }4、执行
彻底明白Android中AIDL及其使用,布布扣,bubuko.com
标签:android des style blog http java
原文地址:http://blog.csdn.net/wangduanqing5945/article/details/35231421