Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (三)
有了Binder程式間通訊介面之後,接下來就是要在Server端實現一個本地服務了。這裡,Server端實現的本地服務名為MemoryService,實現在src/shy/luo/ashmem/MemoryService.java檔案中:
- package shy.luo.ashmem;
- import java.io.FileDescriptor;
- import java.io.IOException;
- import android.os.Parcel;
- import android.os.MemoryFile;
- import android.os.ParcelFileDescriptor;
- import android.util.Log;
- public class MemoryService extends IMemoryService.Stub {
- private final static String LOG_TAG = "shy.luo.ashmem.MemoryService";
- private MemoryFile file = null;
- public MemoryService() {
- try {
- file = new MemoryFile("Ashmem", 4);
- setValue(0);
- }
- catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to create memory file.");
- ex.printStackTrace();
- }
- }
- public ParcelFileDescriptor getFileDescriptor() {
- Log.i(LOG_TAG, "Get File Descriptor.");
- ParcelFileDescriptor pfd = null;
- try {
- pfd = file.getParcelFileDescriptor();
- } catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to get file descriptor.");
- ex.printStackTrace();
- }
- return pfd;
- }
- public void setValue(int val) {
- if(file == null) {
- return;
- }
- byte[] buffer = new byte[4];
- buffer[0] = (byte)((val >>> 24) & 0xFF);
- buffer[1] = (byte)((val >>> 16) & 0xFF);
- buffer[2] = (byte)((val >>> 8) & 0xFF);
- buffer[3] = (byte)(val & 0xFF);
- try {
- file.writeBytes(buffer, 0, 0, 4);
- Log.i(LOG_TAG, "Set value " + val + " to memory file. ");
- }
- catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to write bytes to memory file.");
- ex.printStackTrace();
- }
- }
- }
- package shy.luo.ashmem;
- import java.io.FileDescriptor;
- import java.io.IOException;
- import android.os.Parcel;
- import android.os.MemoryFile;
- import android.os.ParcelFileDescriptor;
- import android.util.Log;
- public class MemoryService extends IMemoryService.Stub {
- private final static String LOG_TAG = "shy.luo.ashmem.MemoryService";
- private MemoryFile file = null;
- public MemoryService() {
- try {
- file = new MemoryFile("Ashmem", 4);
- setValue(0);
- }
- catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to create memory file.");
- ex.printStackTrace();
- }
- }
- public ParcelFileDescriptor getFileDescriptor() {
- Log.i(LOG_TAG, "Get File Descriptor.");
- ParcelFileDescriptor pfd = null;
- try {
- pfd = file.getParcelFileDescriptor();
- } catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to get file descriptor.");
- ex.printStackTrace();
- }
- return pfd;
- }
- public void setValue(int val) {
- if(file == null) {
- return;
- }
- byte[] buffer = new byte[4];
- buffer[0] = (byte)((val >>> 24) & 0xFF);
- buffer[1] = (byte)((val >>> 16) & 0xFF);
- buffer[2] = (byte)((val >>> 8) & 0xFF);
- buffer[3] = (byte)(val & 0xFF);
- try {
- file.writeBytes(buffer, 0, 0, 4);
- Log.i(LOG_TAG, "Set value " + val + " to memory file. ");
- }
- catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to write bytes to memory file.");
- ex.printStackTrace();
- }
- }
- }
package shy.luo.ashmem; import java.io.FileDescriptor; import java.io.IOException; import android.os.Parcel; import android.os.MemoryFile; import android.os.ParcelFileDescriptor; import android.util.Log; public class MemoryService extends IMemoryService.Stub { private final static String LOG_TAG = "shy.luo.ashmem.MemoryService"; private MemoryFile file = null; public MemoryService() { try { file = new MemoryFile("Ashmem", 4); setValue(0); } catch(IOException ex) { Log.i(LOG_TAG, "Failed to create memory file."); ex.printStackTrace(); } } public ParcelFileDescriptor getFileDescriptor() { Log.i(LOG_TAG, "Get File Descriptor."); ParcelFileDescriptor pfd = null; try { pfd = file.getParcelFileDescriptor(); } catch(IOException ex) { Log.i(LOG_TAG, "Failed to get file descriptor."); ex.printStackTrace(); } return pfd; } public void setValue(int val) { if(file == null) { return; } byte[] buffer = new byte[4]; buffer[0] = (byte)((val >>> 24) & 0xFF); buffer[1] = (byte)((val >>> 16) & 0xFF); buffer[2] = (byte)((val >>> 8) & 0xFF); buffer[3] = (byte)(val & 0xFF); try { file.writeBytes(buffer, 0, 0, 4); Log.i(LOG_TAG, "Set value " + val + " to memory file. "); } catch(IOException ex) { Log.i(LOG_TAG, "Failed to write bytes to memory file."); ex.printStackTrace(); } } }注意,這裡的MemoryService類實現了IMemoryService.Stub類,表示這是一個Binder服務的本地實現。在建構函式中,透過指定檔名和檔案大小來建立了一個匿名共享記憶體檔案,即建立MemoryFile的一個例項,並儲存在類成員變數file中。這個匿名共享記憶體檔名為"Ashmem",大小為4個節字,剛好容納一個整數,我們這裡舉的例子就是要說明如果建立一個匿名共享記憶體來在兩個程式間實現共享一個整數了。當然,在實際應用中,可以根據需要建立合適大小的共享記憶體來共享有意義的資料。
這裡還實現了IMemoryService.Stub的兩個介面getFileDescriptor和setVal,一個用來獲取匿名共享記憶體檔案的檔案描述符,一個來往匿名共享記憶體檔案中寫入一個整數,其中,介面getFileDescriptor的返回值是一個ParcelFileDescriptor。在Java中,是用FileDescriptor類來表示一個檔案描述符的,而ParcelFileDescriptor是用來序列化FileDescriptor的,以便在程式間呼叫時傳輸。
定義好本地服務好,就要定義一個Server來啟動這個服務了。這裡定義的Server實現在src/shy/luo/ashmem/Server.java檔案中:
- package shy.luo.ashmem;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- import android.os.ServiceManager;
- public class Server extends Service {
- private final static String LOG_TAG = "shy.luo.ashmem.Server";
- private MemoryService memoryService = null;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- Log.i(LOG_TAG, "Create Memory Service...");
- memoryService = new MemoryService();
- try {
- ServiceManager.addService("AnonymousSharedMemory", memoryService);
- Log.i(LOG_TAG, "Succeed to add memory service.");
- } catch (RuntimeException ex) {
- Log.i(LOG_TAG, "Failed to add Memory Service.");
- ex.printStackTrace();
- }
- }
- @Override
- public void onStart(Intent intent, int startId) {
- Log.i(LOG_TAG, "Start Memory Service.");
- }
- @Override
- public void onDestroy() {
- Log.i(LOG_TAG, "Destroy Memory Service.");
- }
- }
- package shy.luo.ashmem;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- import android.os.ServiceManager;
- public class Server extends Service {
- private final static String LOG_TAG = "shy.luo.ashmem.Server";
- private MemoryService memoryService = null;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- Log.i(LOG_TAG, "Create Memory Service...");
- memoryService = new MemoryService();
- try {
- ServiceManager.addService("AnonymousSharedMemory", memoryService);
- Log.i(LOG_TAG, "Succeed to add memory service.");
- } catch (RuntimeException ex) {
- Log.i(LOG_TAG, "Failed to add Memory Service.");
- ex.printStackTrace();
- }
- }
- @Override
- public void onStart(Intent intent, int startId) {
- Log.i(LOG_TAG, "Start Memory Service.");
- }
- @Override
- public void onDestroy() {
- Log.i(LOG_TAG, "Destroy Memory Service.");
- }
- }
package shy.luo.ashmem; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.os.ServiceManager; public class Server extends Service { private final static String LOG_TAG = "shy.luo.ashmem.Server"; private MemoryService memoryService = null; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Log.i(LOG_TAG, "Create Memory Service..."); memoryService = new MemoryService(); try { ServiceManager.addService("AnonymousSharedMemory", memoryService); Log.i(LOG_TAG, "Succeed to add memory service."); } catch (RuntimeException ex) { Log.i(LOG_TAG, "Failed to add Memory Service."); ex.printStackTrace(); } } @Override public void onStart(Intent intent, int startId) { Log.i(LOG_TAG, "Start Memory Service."); } @Override public void onDestroy() { Log.i(LOG_TAG, "Destroy Memory Service."); } }這個Server繼承了Android系統應用程式框架層提供的Service類,當它被啟動時,執行在一個獨立的程式中。當這個Server被啟動時,它的onCreate函式就會被呼叫,然後它就透過ServiceManager的addService介面來新增MemoryService了:
- memoryService = new MemoryService();
- try {
- ServiceManager.addService("AnonymousSharedMemory", memoryService);
- Log.i(LOG_TAG, "Succeed to add memory service.");
- } catch (RuntimeException ex) {
- Log.i(LOG_TAG, "Failed to add Memory Service.");
- ex.printStackTrace();
- }
- memoryService = new MemoryService();
- try {
- ServiceManager.addService("AnonymousSharedMemory", memoryService);
- Log.i(LOG_TAG, "Succeed to add memory service.");
- } catch (RuntimeException ex) {
- Log.i(LOG_TAG, "Failed to add Memory Service.");
- ex.printStackTrace();
- }
memoryService = new MemoryService(); try { ServiceManager.addService("AnonymousSharedMemory", memoryService); Log.i(LOG_TAG, "Succeed to add memory service."); } catch (RuntimeException ex) { Log.i(LOG_TAG, "Failed to add Memory Service."); ex.printStackTrace(); }這樣,當這個Server成功啟動了,Client就可以透過ServiceManager的getService介面來獲取這個MemoryService了。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13164110/viewspace-747973/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃Android記憶體
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (五)Android記憶體
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (四)Android記憶體
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (二)Android記憶體
- Android匿名共享記憶體(Ashmem)原理Android記憶體
- Android系統中的廣播(Broadcast)機制簡要介紹和學習計劃 .AndroidAST
- Dalvik虛擬機器簡要介紹和學習計劃虛擬機
- Android程式間通訊(IPC)機制Binder簡要介紹和學習計劃Android
- ASMM (Auto Shared Memory Manangement) 自動共享記憶體管理ASMNaN記憶體
- ART執行時垃圾收集機制簡要介紹和學習計劃
- Dalvik虛擬機器垃圾收集機制簡要介紹和學習計劃虛擬機
- javascript記憶體管理簡單介紹JavaScript記憶體
- javascript 記憶體使用管理簡單介紹JavaScript記憶體
- 計算機記憶體管理介紹計算機記憶體
- memory儲存引擎 /MySQL記憶體表的特性與使用介紹儲存引擎MySql記憶體
- Linux系統程式設計—共享記憶體之mmapLinux程式設計記憶體
- CUDA記憶體介紹記憶體
- 記憶體回收介紹記憶體
- 【讀書筆記】Android的Ashmem機制學習筆記Android
- 作業系統——記憶體管理學習筆記作業系統記憶體筆記
- 學習Python要多久?三種方式學習週期介紹!Python
- jvm堆記憶體和GC簡介JVM記憶體GC
- JVM中記憶體和GC的介紹JVM記憶體GC
- 記憶體檔案系統的再學習記憶體
- Unix高階程式設計學習筆記--系統呼叫簡介程式設計筆記
- 記憶體管理簡介記憶體
- linux作業系統修改共享記憶體的簡單方法(轉)Linux作業系統記憶體
- 程式-IPC 共享記憶體和訊息佇列 (三)記憶體佇列
- Linux系統和核心初始化過程簡要介紹(轉)Linux
- [CareerCup] 8.9 An In-memory File System 記憶體檔案系統記憶體
- Linux系統程式設計之命名管道與共享記憶體Linux程式設計記憶體
- javascript匿名函式簡單介紹JavaScript函式
- Bootstrap速學教程之簡要介紹boot
- ros學習檔案系統介紹ROS
- Sieve—Android 記憶體分析系統Android記憶體
- MQTT簡要介紹MQQT
- STM32記憶體結構介紹和FreeRTOS記憶體分配技巧記憶體
- Android WebView Memory Leak WebView記憶體洩漏AndroidWebView記憶體