BREW的接口较多,函数的参数一般都很长,靠记忆往往出错,最有效的方法就是拷贝粘贴了,希望下面的内容可以帮助大家提高编程效率
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));
#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 );
int nNumber;
char strSrc;
nNumber = ATOI(strSrc);
#define PRE_FIX_BAR “bar”
const char strSrc[10];
if( STRBEGINS(PRE_FIX_BAR, strSrc) )
{
// Yes, string begin with PRE_FIX_BAR
}
#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
}
#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);
AECHAR wstSource [MAX_COUNT_SOURCE];
SNPRINTF(buf, sizeof(buf), "%d", 100 );
注意:最好不用SPRINTF,避免内存越界
int m_FontHeight;
int pnAscent;
int pnDescent;
pMe->m_FontHeight = IDISPLAY_GetFontMetrics (pMe->a.m_pIDisplay, AEE_FONT_NORMAL,
&pnAscent, &pnDescent);
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);
// 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 );
}
#include "AEEStdlib.h"
#ifndef CAST
#define CAST(t,o) (t)((void*)o)
#endif
MyAppStruct *pMe = CAST(MyAppStruct *, GETAPPINSTANCE());
IShell * pIShell= ((AEEApplet *)GETAPPINSTANCE())->a.m_pIShell;
#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));
}
}
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
具有NULL结尾
int esn;
char szBuf[8+1];
SNPRINTF(szBuf, sizeof(szBuf), "%8x", esn);
pMe = MALLOCREC(MeStruct);
等效于
pMe = (struct MeStruct *)MALLOC(sizeof(struct MeStruct));
#include "AEEFile.h"
IFileMgr *m_pIFileMgr;
if( ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_FILEMGR, (void **)&pMe->m_pIFileMgr) == SUCCESS )
{
return AEE_ECLASSNOTSUPPORT;
}
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);
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);
if( IFILEMGR_Test(pMe->m_pIFileMgr, szFileName) == SUCCESS)
{
IFILEMGR_Remove(pMe->m_pIFileMgr, szFileName);
}
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);
SETAEERECT(&rcInput, x, y, dx, dy);
ISHELL_StartApplet(pMe->a.m_pIShell, AEECLSID_MY_APP);
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;
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)
{
// 字串相同
}
#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;