Chinaunix首页 | 论坛 | 博客
  • 博客访问: 79108
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 599
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-27 16:46
个人简介

哎……马上就要毕业了!求工作啊^0^

文章分类

全部博文(8)

文章存档

2013年(8)

我的朋友

分类: Android平台

2013-06-28 14:35:12

从本地代码引用java对象:所有的java对象在jni中有相同的表示→jobject

    在这部分,我们关注的是如何存储一个对象在native端,和如何从native端取回一个对象送往java端;案列将定义一个Color类

Saving a reference to an object in the Store---------------------------------------------

1.定义一个新的类:Color;它包含一个了表示color的Integer,这个Integer将被转换成String(列如“#FF0000”);

点击(此处)折叠或打开

  1. package cn.StoreActivity;
  2. public class Color {
  3.     private int mColor;    
  4.     public Color(String color) {
  5.         super();
  6.         mColor = android.graphics.Color.parseColor(color);
  7.     }    
  8.     public String toString(){
  9.         return String.format("#%06X",mColor);
  10.     }
  11. }

2.在StoreType的枚举类型中增加一个Color类型

点击(此处)折叠或打开

  1. package cn.StoreActivity;
  2. public enum StoreType {
  3.   Integer,String,Color;
  4. }
3.Store.java在原来的基础上增加两个native方法来取回和设置Color对象

点击(此处)折叠或打开

  1. public class Store {    
  2.     static{
  3.         System.loadLibrary("store");
  4.     }    
  5. .............................
  6.     public native Color getColor(String key);
  7.     public native void setColor(String key,Color color);

  8. }
4.在jni/Store.h中;增加一个新的color类型到StoreType中和添加一个新成员到StoreValue union;

点击(此处)折叠或打开

  1. ......
  2. typedef enum {
  3.     StoreType_Integer, StoreType_String, StoreType_Color
  4. } StoreType;


  5. typedef union {
  6.     int32_t mInteger;
  7.     char* mString;
  8.     jobject mColor;
  9. } StoreValue;
5.重新用javah编译store.java,得到jni/cn_StoreActivity_Store.h,并在StoreActivity中实现两个新方法;

点击(此处)折叠或打开

  1. ........
  2. JNIEXPORT void JNICALL Java_cn_StoreActivity_Store_setColor
  3.   (JNIEnv* pEnv, jobject pThis, jstring pKey, jobject pColor) {
  4.     // The Java Color is going to be stored on the native side.
  5.     // Need to keep a global reference to avoid a potential
  6.     // garbage collection after method returns.
  7.     jobject lColor = (*pEnv)->NewGlobalRef(pEnv, pColor);
  8.     if (lColor == NULL) {
  9.         return;
  10.     }

  11.     // Save the Color reference in the store.
  12.     StoreEntry* lEntry = allocateEntry(pEnv, &mStore, pKey);
  13.     if (lEntry != NULL) {
  14.         lEntry->mType = StoreType_Color;
  15.         lEntry->mValue.mColor = lColor;
  16.     } else {
  17.         (*pEnv)->DeleteGlobalRef(pEnv, lColor);
  18.     }
  19. }
6.使用NewGlobalRef()之后就必须对应的使用DeleteGlobalRef()。在我们的例子中当entry被替换的时候就必须删除全局引用。在Store.c里面修改releaseEntryValue();

点击(此处)折叠或打开

  1. void releaseEntryValue(JNIEnv* pEnv, StoreEntry* pEntry) {
  2.     switch (pEntry->mType) {
  3.         case StoreType_String:
  4.             free(pEntry->mValue.mString);
  5.             break;
  6.         case StoreType_Color:
  7.             // Unreferences the Id object for garbage collection.
  8.             (*pEnv)->DeleteGlobalRef(pEnv, pEntry->mValue.mColor);
  9.             break;
  10.     }
  11. }
7.使用ndk-build重新编译生成新的so库后,接下来就可以编写Activity端的代码了

点击(此处)折叠或打开

  1. package cn.StoreActivity;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Spinner;
  10. import android.widget.Toast;

  11. public class StoreActivity extends Activity {
  12.     /** Called when the activity is first created. */
  13.     private EditText et_key,et_value;
  14.     private Button bt_getValue,bt_setValue;
  15.     /*A view that displays one child at a time and lets the user pick
  16.     among them. The items in the Spinner come from the Adapter
  17.     associated with this view.*/
  18.     private Spinner spinner;
  19.     private Store store;
  20.     
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.         et_key = (EditText) this.findViewById(R.id.et_Key);
  26.         et_value = (EditText) this.findViewById(R.id.et_Value);
  27.         
  28.         ArrayAdapter<StoreType> adapter = new ArrayAdapter<StoreType>(this,
  29.                 android.R.layout.simple_spinner_item, StoreType.values());
  30.         //Sets the layout resource to create the drop down views.
  31.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  32.         
  33.         spinner = (Spinner) this.findViewById(R.id.sp_Type);
  34.         spinner.setAdapter(adapter);
  35.         bt_getValue = (Button) this.findViewById(R.id.bt_GetValue);
  36.         bt_getValue.setOnClickListener(new OnClickListener() {
  37.             
  38.             @Override
  39.             public void onClick(View v) {
  40.                 onGetValue();
  41.                 
  42.             }
  43.         });
  44.         
  45.         bt_setValue = (Button)this.findViewById(R.id.bt_SetValue);
  46.         bt_setValue.setOnClickListener(new OnClickListener() {
  47.             
  48.             @Override
  49.             public void onClick(View v) {
  50.                 onSetValue();
  51.                 
  52.             }
  53.         });
  54.         store = new Store();//不要忘了初始化!
  55.     }
  56.     public void onGetValue(){
  57.         String key = et_key.getText().toString().trim();
  58.         StoreType type = (StoreType)spinner.getSelectedItem();
  59.         switch (type) {
  60.         case Integer:
  61.             et_value.setText(Integer.toString(store.getInteger(key)));
  62.             break;
  63.         case String:
  64.             et_value.setText(store.getString(key));
  65.             break;
  66.         case Color:
  67.             et_value.setText(store.getColor(key).toString());
  68.             break;
  69.         }
  70.         
  71.     }
  72.     
  73.     public void onSetValue(){
  74.         String key = et_key.getText().toString().trim();
  75.         String value = et_value.getText().toString().trim();
  76.         StoreType type = (StoreType)spinner.getSelectedItem();
  77.         try {
  78.             switch (type) {
  79.             case Integer:
  80.                 store.setInteger(key, Integer.parseInt(value));
  81.                 break;
  82.             case String:
  83.                 store.setString(key,value);
  84.                 break;
  85.             case Color:
  86.                 store.setColor(key, new Color(value));
  87.                 break;
  88.             }
  89.         } catch (Exception e) {
  90.             // TODO Auto-generated catch block
  91.             Toast.makeText(getApplicationContext(), "incorrect value", 0).show();
  92.         }
  93.     }
  94. }
8.运行程序,在Value中输入#FF0000
阅读(1859) | 评论(0) | 转发(0) |
0

上一篇:第三章笔记(1)

下一篇:交换类排序

给主人留下些什么吧!~~