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)原理Android記憶體
- Android記憶體優化(四)解析Memory Monitor、Allocation Tracker和Heap DumpAndroid記憶體優化
- Linux系統程式設計—共享記憶體之mmapLinux程式設計記憶體
- 計算機記憶體管理介紹計算機記憶體
- CUDA記憶體介紹記憶體
- 記憶體回收介紹記憶體
- JVM中記憶體和GC的介紹JVM記憶體GC
- jvm堆記憶體和GC簡介JVM記憶體GC
- Sieve—Android 記憶體分析系統Android記憶體
- Android Studio Profiler Memory (記憶體分析工具)的簡單使用及問題Android記憶體
- 作業系統——記憶體管理學習筆記作業系統記憶體筆記
- Linux系統程式設計之命名管道與共享記憶體Linux程式設計記憶體
- STM32記憶體結構介紹和FreeRTOS記憶體分配技巧記憶體
- 記憶體檔案系統的再學習記憶體
- Unix高階程式設計學習筆記--系統呼叫簡介程式設計筆記
- Memory記憶體傳值記憶體
- VisionPro學習筆記(1)——軟體介紹和基本使用筆記
- 【Android系統】Android系統架構簡介Android架構
- Java記憶體模型簡介Java記憶體模型
- Java記憶體模型 - 簡介Java記憶體模型
- Win10系統GPU共享記憶體怎麼關閉?Win10系統GPU共享記憶體的關閉方法Win10GPU記憶體
- ros學習檔案系統介紹ROS
- Android 輸入系統介紹Android
- Android系統介紹與框架Android框架
- Unity Memory Profiler 記憶體分析Unity記憶體
- Allowed memory size 記憶體不足記憶體
- 記憶體管理(Debug Memory Graph)記憶體
- 元學習簡單介紹
- 持久記憶體指令(PMDK)簡介記憶體
- Linux系統的發展歷史和學習前景介紹Linux
- 學習筆記-React的簡單介紹&工作原理筆記React
- [Linux]共享記憶體Linux記憶體
- POSIX 共享記憶體記憶體
- Golang 共享記憶體Golang記憶體
- 12. 記憶體管理(Memory Management)記憶體
- 共享記憶體對映(linux程式與執行緒學習筆記)記憶體Linux執行緒筆記
- 知名駭客組織Anonymous(匿名者)
- MySQL記憶體管理,記憶體分配器和作業系統MySql記憶體作業系統
- ClickHouse學習系列之七【系統命令介紹】