Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (四)
接著,我們就來看Client端的實現。Client端是一個Activity,實現在src/shy/luo/ashmem/Client.java檔案中:
- package shy.luo.ashmem;
- import java.io.FileDescriptor;
- import java.io.IOException;
- import shy.luo.ashmem.R;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.MemoryFile;
- import android.os.ParcelFileDescriptor;
- import android.os.ServiceManager;
- import android.os.RemoteException;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class Client extends Activity implements OnClickListener {
- private final static String LOG_TAG = "shy.luo.ashmem.Client";
- IMemoryService memoryService = null;
- MemoryFile memoryFile = null;
- private EditText valueText = null;
- private Button readButton = null;
- private Button writeButton = null;
- private Button clearButton = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- IMemoryService ms = getMemoryService();
- if(ms == null) {
- startService(new Intent("shy.luo.ashmem.server"));
- } else {
- Log.i(LOG_TAG, "Memory Service has started.");
- }
- valueText = (EditText)findViewById(R.id.edit_value);
- readButton = (Button)findViewById(R.id.button_read);
- writeButton = (Button)findViewById(R.id.button_write);
- clearButton = (Button)findViewById(R.id.button_clear);
- readButton.setOnClickListener(this);
- writeButton.setOnClickListener(this);
- clearButton.setOnClickListener(this);
- Log.i(LOG_TAG, "Client Activity Created.");
- }
- @Override
- public void onResume() {
- super.onResume();
- Log.i(LOG_TAG, "Client Activity Resumed.");
- }
- @Override
- public void onPause() {
- super.onPause();
- Log.i(LOG_TAG, "Client Activity Paused.");
- }
- @Override
- public void onClick(View v) {
- if(v.equals(readButton)) {
- int val = 0;
- MemoryFile mf = getMemoryFile();
- if(mf != null) {
- try {
- byte[] buffer = new byte[4];
- mf.readBytes(buffer, 0, 0, 4);
- val = (buffer[0] <24) | ((buffer[1] & 0xFF) <16) | ((buffer[2] & 0xFF) <8) | (buffer[3] & 0xFF);
- } catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to read bytes from memory file.");
- ex.printStackTrace();
- }
- }
- String text = String.valueOf(val);
- valueText.setText(text);
- } else if(v.equals(writeButton)) {
- String text = valueText.getText().toString();
- int val = Integer.parseInt(text);
- IMemoryService ms = getMemoryService();
- if(ms != null) {
- try {
- ms.setValue(val);
- } catch(RemoteException ex) {
- Log.i(LOG_TAG, "Failed to set value to memory service.");
- ex.printStackTrace();
- }
- }
- } else if(v.equals(clearButton)) {
- String text = "";
- valueText.setText(text);
- }
- }
- private IMemoryService getMemoryService() {
- if(memoryService != null) {
- return memoryService;
- }
- memoryService = IMemoryService.Stub.asInterface(
- ServiceManager.getService("AnonymousSharedMemory"));
- Log.i(LOG_TAG, memoryService != null ? "Succeed to get memeory service." : "Failed to get memory service.");
- return memoryService;
- }
- private MemoryFile getMemoryFile() {
- if(memoryFile != null) {
- return memoryFile;
- }
- IMemoryService ms = getMemoryService();
- if(ms != null) {
- try {
- ParcelFileDescriptor pfd = ms.getFileDescriptor();
- if(pfd == null) {
- Log.i(LOG_TAG, "Failed to get memory file descriptor.");
- return null;
- }
- try {
- FileDescriptor fd = pfd.getFileDescriptor();
- if(fd == null) {
- Log.i(LOG_TAG, "Failed to get memeory file descriptor.");
- return null;
- }
- memoryFile = new MemoryFile(fd, 4, "r");
- } catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to create memory file.");
- ex.printStackTrace();
- }
- } catch(RemoteException ex) {
- Log.i(LOG_TAG, "Failed to get file descriptor from memory service.");
- ex.printStackTrace();
- }
- }
- return memoryFile;
- }
- }
- package shy.luo.ashmem;
- import java.io.FileDescriptor;
- import java.io.IOException;
- import shy.luo.ashmem.R;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.MemoryFile;
- import android.os.ParcelFileDescriptor;
- import android.os.ServiceManager;
- import android.os.RemoteException;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class Client extends Activity implements OnClickListener {
- private final static String LOG_TAG = "shy.luo.ashmem.Client";
- IMemoryService memoryService = null;
- MemoryFile memoryFile = null;
- private EditText valueText = null;
- private Button readButton = null;
- private Button writeButton = null;
- private Button clearButton = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- IMemoryService ms = getMemoryService();
- if(ms == null) {
- startService(new Intent("shy.luo.ashmem.server"));
- } else {
- Log.i(LOG_TAG, "Memory Service has started.");
- }
- valueText = (EditText)findViewById(R.id.edit_value);
- readButton = (Button)findViewById(R.id.button_read);
- writeButton = (Button)findViewById(R.id.button_write);
- clearButton = (Button)findViewById(R.id.button_clear);
- readButton.setOnClickListener(this);
- writeButton.setOnClickListener(this);
- clearButton.setOnClickListener(this);
- Log.i(LOG_TAG, "Client Activity Created.");
- }
- @Override
- public void onResume() {
- super.onResume();
- Log.i(LOG_TAG, "Client Activity Resumed.");
- }
- @Override
- public void onPause() {
- super.onPause();
- Log.i(LOG_TAG, "Client Activity Paused.");
- }
- @Override
- public void onClick(View v) {
- if(v.equals(readButton)) {
- int val = 0;
- MemoryFile mf = getMemoryFile();
- if(mf != null) {
- try {
- byte[] buffer = new byte[4];
- mf.readBytes(buffer, 0, 0, 4);
- val = (buffer[0] <24) | ((buffer[1] & 0xFF) <16) | ((buffer[2] & 0xFF) <8) | (buffer[3] & 0xFF);
- } catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to read bytes from memory file.");
- ex.printStackTrace();
- }
- }
- String text = String.valueOf(val);
- valueText.setText(text);
- } else if(v.equals(writeButton)) {
- String text = valueText.getText().toString();
- int val = Integer.parseInt(text);
- IMemoryService ms = getMemoryService();
- if(ms != null) {
- try {
- ms.setValue(val);
- } catch(RemoteException ex) {
- Log.i(LOG_TAG, "Failed to set value to memory service.");
- ex.printStackTrace();
- }
- }
- } else if(v.equals(clearButton)) {
- String text = "";
- valueText.setText(text);
- }
- }
- private IMemoryService getMemoryService() {
- if(memoryService != null) {
- return memoryService;
- }
- memoryService = IMemoryService.Stub.asInterface(
- ServiceManager.getService("AnonymousSharedMemory"));
- Log.i(LOG_TAG, memoryService != null ? "Succeed to get memeory service." : "Failed to get memory service.");
- return memoryService;
- }
- private MemoryFile getMemoryFile() {
- if(memoryFile != null) {
- return memoryFile;
- }
- IMemoryService ms = getMemoryService();
- if(ms != null) {
- try {
- ParcelFileDescriptor pfd = ms.getFileDescriptor();
- if(pfd == null) {
- Log.i(LOG_TAG, "Failed to get memory file descriptor.");
- return null;
- }
- try {
- FileDescriptor fd = pfd.getFileDescriptor();
- if(fd == null) {
- Log.i(LOG_TAG, "Failed to get memeory file descriptor.");
- return null;
- }
- memoryFile = new MemoryFile(fd, 4, "r");
- } catch(IOException ex) {
- Log.i(LOG_TAG, "Failed to create memory file.");
- ex.printStackTrace();
- }
- } catch(RemoteException ex) {
- Log.i(LOG_TAG, "Failed to get file descriptor from memory service.");
- ex.printStackTrace();
- }
- }
- return memoryFile;
- }
- }
package shy.luo.ashmem; import java.io.FileDescriptor; import java.io.IOException; import shy.luo.ashmem.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.MemoryFile; import android.os.ParcelFileDescriptor; import android.os.ServiceManager; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Client extends Activity implements OnClickListener { private final static String LOG_TAG = "shy.luo.ashmem.Client"; IMemoryService memoryService = null; MemoryFile memoryFile = null; private EditText valueText = null; private Button readButton = null; private Button writeButton = null; private Button clearButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); IMemoryService ms = getMemoryService(); if(ms == null) { startService(new Intent("shy.luo.ashmem.server")); } else { Log.i(LOG_TAG, "Memory Service has started."); } valueText = (EditText)findViewById(R.id.edit_value); readButton = (Button)findViewById(R.id.button_read); writeButton = (Button)findViewById(R.id.button_write); clearButton = (Button)findViewById(R.id.button_clear); readButton.setOnClickListener(this); writeButton.setOnClickListener(this); clearButton.setOnClickListener(this); Log.i(LOG_TAG, "Client Activity Created."); } @Override public void onResume() { super.onResume(); Log.i(LOG_TAG, "Client Activity Resumed."); } @Override public void onPause() { super.onPause(); Log.i(LOG_TAG, "Client Activity Paused."); } @Override public void onClick(View v) { if(v.equals(readButton)) { int val = 0; MemoryFile mf = getMemoryFile(); if(mf != null) { try { byte[] buffer = new byte[4]; mf.readBytes(buffer, 0, 0, 4); val = (buffer[0] << 24) | ((buffer[1] & 0xFF) << 16) | ((buffer[2] & 0xFF) << 8) | (buffer[3] & 0xFF); } catch(IOException ex) { Log.i(LOG_TAG, "Failed to read bytes from memory file."); ex.printStackTrace(); } } String text = String.valueOf(val); valueText.setText(text); } else if(v.equals(writeButton)) { String text = valueText.getText().toString(); int val = Integer.parseInt(text); IMemoryService ms = getMemoryService(); if(ms != null) { try { ms.setValue(val); } catch(RemoteException ex) { Log.i(LOG_TAG, "Failed to set value to memory service."); ex.printStackTrace(); } } } else if(v.equals(clearButton)) { String text = ""; valueText.setText(text); } } private IMemoryService getMemoryService() { if(memoryService != null) { return memoryService; } memoryService = IMemoryService.Stub.asInterface( ServiceManager.getService("AnonymousSharedMemory")); Log.i(LOG_TAG, memoryService != null ? "Succeed to get memeory service." : "Failed to get memory service."); return memoryService; } private MemoryFile getMemoryFile() { if(memoryFile != null) { return memoryFile; } IMemoryService ms = getMemoryService(); if(ms != null) { try { ParcelFileDescriptor pfd = ms.getFileDescriptor(); if(pfd == null) { Log.i(LOG_TAG, "Failed to get memory file descriptor."); return null; } try { FileDescriptor fd = pfd.getFileDescriptor(); if(fd == null) { Log.i(LOG_TAG, "Failed to get memeory file descriptor."); return null; } memoryFile = new MemoryFile(fd, 4, "r"); } catch(IOException ex) { Log.i(LOG_TAG, "Failed to create memory file."); ex.printStackTrace(); } } catch(RemoteException ex) { Log.i(LOG_TAG, "Failed to get file descriptor from memory service."); ex.printStackTrace(); } } return memoryFile; } }Client端的介面主要包含了三個按鈕Read、Write和Clear,以及一個用於顯示內容的文字框。
這個Activity在onCreate時,會透過startService介面來啟動我們前面定義的Server程式。呼叫startService時,需要指定要啟動的服務的名稱,這裡就是"shy.luo.ashmem.server"了,後面我們會在程式的描述檔案AndroidManifest.xml看到前面的Server類是如何和名稱"shy.luo.ashmem.server"關聯起來的。關於呼叫startService函式來啟動自定義服務的過程,可以參考Android系統在新程式中啟動自定義服務過程(startService)的原理分析一文。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13164110/viewspace-747974/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 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虛擬機器垃圾收集機制簡要介紹和學習計劃虛擬機
- Android記憶體優化(四)解析Memory Monitor、Allocation Tracker和Heap DumpAndroid記憶體優化
- javascript記憶體管理簡單介紹JavaScript記憶體
- javascript 記憶體使用管理簡單介紹JavaScript記憶體
- 計算機記憶體管理介紹計算機記憶體
- memory儲存引擎 /MySQL記憶體表的特性與使用介紹儲存引擎MySql記憶體
- Linux系統程式設計—共享記憶體之mmapLinux程式設計記憶體
- CUDA記憶體介紹記憶體
- 記憶體回收介紹記憶體
- 【讀書筆記】Android的Ashmem機制學習筆記Android
- 作業系統——記憶體管理學習筆記作業系統記憶體筆記
- jvm堆記憶體和GC簡介JVM記憶體GC
- JVM中記憶體和GC的介紹JVM記憶體GC
- 記憶體檔案系統的再學習記憶體
- Unix高階程式設計學習筆記--系統呼叫簡介程式設計筆記
- 記憶體管理簡介記憶體
- linux作業系統修改共享記憶體的簡單方法(轉)Linux作業系統記憶體
- 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記憶體
- Win10系統GPU共享記憶體怎麼關閉?Win10系統GPU共享記憶體的關閉方法Win10GPU記憶體