由于安卓系統顯示的emoji表情不那么好看,所以在項目的聊天里面使用了自己的圖片代替了系統的emoji顯示,但是問題來了,在收到消息通知的時候,通知欄并沒有顯示自定義的emoji顯示,而是顯示了系統的emoji表情,沒辦法,只好上網查一下解決方法,最終成功將emoji表情轉化為“[表情]”。
關鍵方法如下:
/**
* 判別是否包含Emoji表情
*
* @param str
* @return
*/
private static boolean containsEmoji(String str)
{
int len = str.length();
for (int i = 0; i < len; i++)
{
if (isEmojiCharacter(str.charAt(i)))
{
return true;
}
}
return false;
}
/**
* 是否是emoji字符
*
* @param codePoint
* @return
*/
private static boolean isEmojiCharacter(char codePoint)
{
return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
|| (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
}
/**
* 將含有EMOJI表情的文字轉換
* @param source
* @return
*/
public static String filterEmoji(String source)
{
if (!containsEmoji(source))
{
//不包含emoji就返回原字符串
return source;
}
StringBuilder buf = null;
//獲取字符串的長度
int len = source.length();
//逐個檢查字符,如果是emoji字符就替換為[表情]
for (int i = 0; i < len; i++)
{
char codePoint = source.charAt(i);
if (!isEmojiCharacter(codePoint))
{
if (buf == null)
{
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
}
else
{
if (buf == null)
{
buf = new StringBuilder(source.length());
}
buf.append("[表情]");
}
}
if (buf == null)
{
return "";
}
else
{
if (buf.length() == len)
{
buf = null;
return source;
}
else
{
return buf.toString();
}
}
}
本文章參考了:http://blog.csdn.net/zhengdan66/article/details/48276209