BREW 编程小提示

来源:互联网 发布:印度人知乎 编辑:程序博客网 时间:2024/06/06 01:32
 

BREW的接口较多,函数的参数一般都很长,靠记忆往往出错,最有效的方法就是拷贝粘贴了,希望下面的内容可以帮助大家提高编程效率

 

1.    字串转换为UNICODE

STRTOWSTR(strInBuf, wstrOutBuf, sizeof(wstrOutBuf));

 

2.    UNICODE转换为字串

WSTRTOSTR(wstrInBuf, strOutBuf, sizeof(strOutBuf));

 

3.    UTF8转换为UNICODE

MEMSET((char *)wstrUnicodeOut, ‘\0’, sizeof(wstrUnicodeOut));

UTF8TOWSTR (strUtf8In, STRLEN(strUtf8In), wstrUnicodeOut, sizeof(wstrUnicodeOut));

注意:UTF8TOWSTR 不自动添加NULL结尾

 

4.    UNICODE转换为UTF8

WSTRTOUTF8(wstrInBuf, WSTRLEN(wstrInBuf),  strOutUtf8, sizeof(strOutUtf8));

     

5.    最小和最大值

#define MIN((A),(B)) ((A)<(B) ? (A) : (B))

#define MAX((A),(B)) ((A)>(B) ? (A) : (B))

 

6.    任意资源的释放方法

#define RELEASEIF(pi)      { if (pi) { IBASE_Release((IBase*)(pi)); (pi)=0; }}

RELEASEIF ( pMe->m_pIFileMgr );

 

7.    字串转换为数字

int nNumber;

char strSrc;

 

nNumber = ATOI(strSrc);

 

8.    检查字串头

#define PRE_FIX_BAR “bar”

const char strSrc[10];

 

if( STRBEGINS(PRE_FIX_BAR, strSrc) )

{

  // Yes, string begin with PRE_FIX_BAR

}

 

9.    查找字母

#define CHAR_TO_FIND ‘:’

char strInput[10];

char *pstrOutput;

 

pstrOutput = STRCHR(strInput, CHAR_TO_FIND);

if( pstrOutput != NULL )

{

  // Yes, find CHAR_TO_FIND in string pstrOutput

}

 

10.  从资源文件中读取图像

#define MY_RES_FILE “myapp.bar”

IImage *pii = NULL;

 

pii = ISHELL_LoadResImage( pIShell, MY_RES_FILE, nResID);

 

11.  char字串拷贝

不保证NULL结尾

char strDest[MAX_COUNT_DEST];

STRNCPY(strDest, strSource, MAX_COUNT_DEST);

  

保证NULL结尾

char strDest[MAX_COUNT_DEST];

STRLCPY(strDest, strSource, MAX_COUNT_DEST);

 

12.  AECHAR字串拷贝

不保证NULL结尾

AECHAR  wstSource [MAX_COUNT_SOURCE];

AECHAR  wstDest[MAX_COUNT_DEST];

 

WSTRNCOPYN(wstDest, MAX_COUNT_DEST, wstSource, MAX_COUNT_SOURCE);

 

如果wstSource是以NULL结尾的情况时:

WSTRNCOPYN(wstDest, MAX_COUNT_DEST_IN_AECHAR, wstSource, -1);

  

保证NULL结尾

AECHAR  wstDest[MAX_COUNT_DEST_IN_WIDECHAR];

 

WSTRLCPY(wstDest, wstSource, MAX_COUNT_DEST_IN_WIDECHAR);

 

13.  AECHAR结尾添加字符

AECHAR  wstSource [MAX_COUNT_SOURCE];

AECHAR  wstDest[MAX_COUNT_DEST];

 

WSTRCAT(wstDest, wstSource)

 

or

 

WSTRLCAT(wstDest, wstSource, MAX_COUNT_DEST_IN_AECHAR);

 

14.  格式化输出

AECHAR  wstSource [MAX_COUNT_SOURCE];

 

SNPRINTF(buf, sizeof(buf), "%d", 100 );

注意:最好不用SPRINTF,避免内存越界

 

15.  获取字体高度

int m_FontHeight;

 

int pnAscent;

int pnDescent;

pMe->m_FontHeight = IDISPLAY_GetFontMetrics (pMe->a.m_pIDisplay, AEE_FONT_NORMAL,

                                               &pnAscent, &pnDescent);

 

16.  获取字串实际的显示宽度

int   pixelWidth;

pixelWidth = IDISPLAY_MeasureText(pMe->a.m_pIDisplay, AEE_FONT_NORMAL, wStr);

 

17.  在特定区域内,可以显示的字串长度

int   nFits;  // [out] 可以显示的字串长度

int   nMaxWidth;  // [in] 特定区域的宽度

 

IDisplay_MeasureTextEx ( pMe->a.m_pIDisplay,

AEE_FONT_NORMAL,

                      wstTextIn,

                      -1,  // 长度由NULL计算获得

                      nMaxWidth,

                      &nFits);

 

18.  启动定时器

// 2000 milliseconds

#define  MY_TIMER_SPAN  (2*1000)

 

ISHELL_SetTimer(pMe->a.m_pIShell, MY_TIMER_SPAN, MyTimerCallBack, pMe);

void MyTimerCallBack(void * po)

{

    CMyApp *pMe = (CMyApp *)po;

    // …   

}

 

取消定时器

 

ISHELL_CancelTimer(pMe->a.m_pIShell, MyTimerCallBack, pMe);

 

19.  获取时间并输出(YYYY/MM/DD  hh:mm:ss)

    {

char szTime[19+1];  // [out]

        JulianType CurDate;

        uint32 dwSecs;

        dwSecs = GETTIMESECONDS(); // returns the number of seconds since 1980/01/06 00:00:00 UTC

        GETJULIANDATE(dwSecs, &CurDate); // to JulianType

        // format out

        SNPRINTF(szTime, sizeof(szTime), "%04d/%02d/%02d %02d:%02d:%02d",

                 CurDate.wYear, CurDate.wMonth, CurDate.wDay,

                 CurDate.wHour, CurDate.wMinute, CurDate.wSecond );

    }

 

20.  获取应用的数据结构指针pMe

#include "AEEStdlib.h"

 

#ifndef CAST

#define CAST(t,o) (t)((void*)o)

#endif

 

MyAppStruct *pMe = CAST(MyAppStruct *, GETAPPINSTANCE());

 

21.  全局获取IShell指针

IShell * pIShell= ((AEEApplet *)GETAPPINSTANCE())->a.m_pIShell;

 

22.  获取IMSI

#include "AEETAPI.h"

 

   TAPIStatus  tpst;

    ITAPI*  pITapi;

    char szIMSI [20];

       

    if( ISHELL_CreateInstance( pMe->a.m_pIShell, AEECLSID_TAPI, (void **)(&pITapi))==SUCCESS )

    {

        if (ITAPI_GetStatus(pITapi, &tpst) == SUCCESS)

        {

            STRLCPY(szIMSI, tpst.szMobileID, sizeof(szIMSI));

        }

    }

 

23.  获取手机平台号

    AEEDeviceInfo  DeviceInfo; // always have access to the hardware device information

 

    pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);

    ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);

 

    // 平台号

    pMe->DeviceInfo.dwPlatformID

 

24.  格式化字串输出(SNPRINTF)

    具有NULL结尾

    int esn;

    char szBuf[8+1];

    SNPRINTF(szBuf, sizeof(szBuf), "%8x", esn);

 

25.  申请内存

    pMe = MALLOCREC(MeStruct);

   等效于

    pMe = (struct MeStruct *)MALLOC(sizeof(struct MeStruct));

 

26.  文件管理器(IFileMgr)创建

    #include "AEEFile.h"

 

   IFileMgr  *m_pIFileMgr;

 

    if( ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_FILEMGR, (void **)&pMe->m_pIFileMgr) == SUCCESS )

    {

        return AEE_ECLASSNOTSUPPORT;

    }

 

27.  创建用于写的文件

    IFile *pIFile;

    if ((pIFile = IFILEMGR_OpenFile(pMe->m_pIFileMgr, MY_FILE_NAME, _OFM_CREATE)) == NULL)

    {

        return EFAILED;

    }

 

    if( IFILE_Write(pIFile, pBuffer, BufferLength)  != BufferLength)

    {

        // something wrong

    }

 

    IFILE_Release(pIFile);

 

28.  打开文件用于读取

    IFile *pIFile;

    char Buffer[BUFFER_SIZE];

    int32  nRead;

 

    if ((pIFile = IFILEMGR_OpenFile(pMe->m_pIFileMgr, MY_FILE_NAME, _OFM_READ)) == NULL)

    {

        return EFAILED;

    }

 

    nRead = IFILE_Read(pUser->pIFileSrcToCopy, Buffer, BUFFER_SIZE);

    if(nRead != 0)

    {

        // process data in Buffer

    }

 

    IFILE_Release(pIFile);

 

29.  删除文件

    if( IFILEMGR_Test(pMe->m_pIFileMgr, szFileName) == SUCCESS)

    {

        IFILEMGR_Remove(pMe->m_pIFileMgr, szFileName);

    }

 

30.  文件信息

  IFile *pIFile;

  FileInfo fiMyFile;

  IFILE_GetInfo(pIFile, &fiMyFile);

 

31.  Bitmap 显示到屏幕

 

        IDISPLAY_BitBlt(pMe->pIDisplay,

            xDest,

            yDest,

            pMe->DeviceInfo.cxScreen,

            pMe->DeviceInfo.cyScreen,

            pBitmap,

            xSrc,

            ySrc,

            AEE_RO_COPY);          

 

32.  设置rect

        SETAEERECT(&rcInput, x, y, dx, dy);

 

33.  启动应用

        ISHELL_StartApplet(pMe->a.m_pIShell, AEECLSID_MY_APP);

 

34.  停止应用

        ISHELL_CloseApplet(pMe->a.m_pIShell, FALSE);

 

35.  处理BREW短信并启动应用

    char *pMsgPayload;

    pMsgPayload = (char*)dwParam;

              

    if ( STRICMP( pMsgPayload, "my_payload" ) == 0)

    {

         ISHELL_StartApplet(pMe->a.m_pIShell, AEECLSID_MY_APP);

    }

    return TRUE;

 

36.  字串比较

    char *pMsg[16];

 

   // 大小写敏感             

   if ( STRCMP(pMsg, "my_payload" ) == 0)

   {

       // 字串相同

   }

 

   // 大小写不敏感              

   if ( STRICMP(pMsg, "my_payload" ) == 0)

   {

       // 字串相同

   }

 

   // 大小写不敏感,只最多比较10个字节             

   if ( STRNICMP(pMsg, "my_payload", 10 ) == 0)

   {

       // 字串相同

   }

 

   // 大小写敏感,只最多比较10个字节             

   if ( STRNCMP(pMsg, "my_payload", 10 ) == 0)

   {

       // 字串相同

   }

            

 

37.  自定义事件

#include “AEEEvent.h”

 

AEEEvent  m_NewFileReady;

 

// regiester

pMe->m_NewFileReady = ISHELL_RegisterEvent(pMe->a.m_pIShell, "NewFileReady", NULL);

if( pMe->m_NewFileReady == NULL)

{

    return EFAILED;

}

 

// send

ISHELL_PostEvent(pMe->a.m_pIShell, AEECLSID_MY_APP, pMe->m_NewFileReady, 0, 0);                       

 

// receive

static boolean MyApp_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)

  if(eCode == pMe->m_NewFileReady)

  {

      // received an user event

           return TRUE;

  }

}

 

 

38.  回调实现

#include “AEECallback.h”

 

AEECallback  m_cbResult;

 

static void MyCallBackFun(void * po)

{

    CMyApp *pMe = (CMyApp *)po;

    ……

}

 

CALLBACK_Init(&pMe-> m_cbResult, MyCallBackFun, pMe);

ISHELL_Resume(pMe->pIShell, &pMe->m_cbResult);

 

39.  防止休眠

case EVT_APP_NO_SLEEP:

    return TRUE;

 

 

原创粉丝点击