AI开发实战11-加密功能插件的开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xjbclz/article/details/78002226

20.1.1 插件的实现

首先需要建立存放插件源码的文件夹:

/appinventor/components/src/com/qz/extensions

然后创建源码文件Encryption.java,其中的代码如下:

//插件的包名,通常是三段式com. + 功能描述. + extension

package com.encryption.extension;    


import android.content.Context;     

import android.util.Log;            

import com.google.appinventor.components.annotations.DesignerComponent;

import com.google.appinventor.components.annotations.SimpleFunction;

import com.google.appinventor.components.annotations.SimpleObject;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.runtime.*;


import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;


@DesignerComponent(

    //插件的版本号

version = Encryption.VERSION,  

//插件的说明文字                         

description = "功能:数据加密 开发者:QZ",     

//如果在网上有插件的说明文字,可以在此设置网址

helpUrl = "http://www.baidu.com",

//设置组件的类型为插件

category = ComponentCategory.EXTENSION,    

//插件是否可见,由于目前App Inventor2只能开发和使用不可见的插件,因此这里只能设置成true                            

nonVisible = true,    

//插件的图标,在此使用App Invento2提供的图标                                                 

    iconName = "images/extension.png") 


//设置让编译器识别到是一个可扩展控件(与前一个注解的Category不同,这一个标识是给编译器处理可扩展控件识别的),然后将其独立打包生成一个aix文件

@SimpleObject(external = true)        

public class Encryption extends AndroidNonvisibleComponent    

implements Component {                                           

    public static final int VERSION = 1;                        

    private ComponentContainer container;                        

    private Context context;                                     

    private static final String LOG_TAG = "Encryption";       

    public Encryption(ComponentContainer container) {          

        super(container.$form());                                

        this.container = container;                              

        context = (Context) container.$context();                

        Log.d(LOG_TAG, "Encryption Created" );                 

    }

    

  //此插件对外提供的函数

    @SimpleFunction(description = "对数据进行MD5加密")

    public String makeMD5Hash(String srcStr) {

      String MD5Str;

      try {

          final MessageDigest mDigest = MessageDigest.getInstance("MD5");

          mDigest.reset();

          mDigest.update(srcStr.getBytes());

          MD5Str = bytesToHexString(mDigest.digest());

      } catch (NoSuchAlgorithmException e) {

          MD5Str = String.valueOf(srcStr.hashCode());

      }

      return MD5Str;

  }

  

  private String bytesToHexString(byte[] bytes) {

      StringBuilder sb = new StringBuilder();

      for (byte value : bytes) {

          String hex = Integer.toHexString(0xFF & value);

          if (hex.length() == 1) {

              sb.append('0');

          }

          sb.append(hex);

      }

      return sb.toString();

  }

}

20.1.2 插件的编译

appinventor目录下执行"ant extensions"命令即可生成相应的插件:com.encryption.extension.aix,生成的文件存放路径如下:

appinventor-sources/appinventor/components/build/extensions/com.encryption.extension.aix

猜你喜欢

转载自blog.csdn.net/xjbclz/article/details/78002226