之前尝试过使用BadgeView来实现,修改涉及地方太多了,后来换了这种方式
本文仅简单实现拨号、短信图标动态提示未读数目,若有更好方式实现的,欢迎交流
Launcher.java
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
......
//ADD BY XIONG.CHEN FOR SHOW UNREAD MMS
else if (msg.what == UPDATE_MMS_ICON) {
setMmsOrPhoneNum(MMS_ICON_NAME, getMissMmsCount());
} else if (msg.what == UPDATE_CALL_ICON) {
setMmsOrPhoneNum(PHONE_ICON_NAME, getMissCallCount());
}
}
};
//ADD BY XIONG.CHEN FOR SHOW UNREAD MMS ADD MISSCALL
private SMSContentObserver smsContentObserver = null;
private CallContentObserver callContentObserver = null;
private final static int UPDATE_MMS_ICON = 826;
private final static int UPDATE_CALL_ICON = 1206;
private final static String PHONE_ICON_NAME = "com.android.contacts.activities.DialtactsActivity";
private final static String MMS_ICON_NAME = "com.android.mms.ui.ConversationList";
public class SMSContentObserver extends ContentObserver {
private Handler mSmsHandler;
public SMSContentObserver(Context context, Handler handler) {
super(handler);
mSmsHandler = handler;
}
@Override
public void onChange(boolean selfChange) {
mHandler.removeMessages(UPDATE_MMS_ICON);
Message msg = mHandler.obtainMessage(UPDATE_MMS_ICON);
msg.obj = getMissCallCount();
mHandler.sendMessage(msg);
}
}
public class CallContentObserver extends ContentObserver {
private Handler mHandler;
public CallContentObserver(Context context, Handler handler) {
super(handler);
mHandler = handler;
}
@Override
public void onChange(boolean selfChange) {
mHandler.removeMessages(UPDATE_CALL_ICON);
Message msg = mHandler.obtainMessage(UPDATE_CALL_ICON);
msg.obj = getMissCallCount();
mHandler.sendMessage(msg);
}
}
/**
*
* @param flag updata which icon,phone or mms
* @param missCount
*/
private void setMmsOrPhoneNum(final String flag, final int missCount) {
if(mWorkspace == null) return;
ArrayList cellLayouts = mWorkspace.getWorkspaceAndHotseatCellLayouts();
for (final CellLayout layoutParent: cellLayouts) {
final ViewGroup shortcutAndWidgetContainer = layoutParent.getShortcutsAndWidgets();
mWorkspace.post(new Runnable() {
public void run() {
int childCount = shortcutAndWidgetContainer.getChildCount();
for (int j = 0; j < childCount; j++) {
View view = shortcutAndWidgetContainer.getChildAt(j);
Object tag = view.getTag();
if (tag instanceof ShortcutInfo) {
final ShortcutInfo info = (ShortcutInfo) tag;
final Intent intent = info.intent;
if (intent != null) {
final ComponentName name = intent.getComponent();
if (name != null && name.getClassName().equals(flag)) {
BubbleTextView bv = (BubbleTextView) view;
Bitmap defaultIconBitmap = Bitmap.createBitmap(info.getIcon(mIconCache));
Bitmap bitmap = Utilities.createIconBitmap(defaultIconBitmap, missCount);
bv.setCompoundDrawablesWithIntrinsicBounds(null,
new FastBitmapDrawable(bitmap),
null, null);
}
}
}
}
}
});
}
}
private int getMissMmsCount() {
int missSmsCount = 0;
Cursor cursorSMS = null;
Cursor cursorMMS = null;
try {
cursorSMS = getContentResolver().query(
Uri.parse("content://sms"), null, "(read=0 and type=1)",
null, null);
cursorMMS = getContentResolver().query(
Uri.parse("content://mms"), null, "(read=0)", null,
null);
} catch (SQLiteException e) {
return missSmsCount;
}
if (cursorSMS != null) {
missSmsCount = cursorSMS.getCount();
cursorSMS.close();
}
if (cursorMMS != null) {
missSmsCount = missSmsCount + cursorMMS.getCount();
cursorMMS.close();
}
return missSmsCount;
}
private int getMissCallCount() {
int missCallCount = 0;
Uri missingCallUri = Calls.CONTENT_URI;
String where = Calls.TYPE + " = '" + Calls.MISSED_TYPE + "'"
+ " AND new=1";
Cursor cursorCall = null;
try {
cursorCall = getContentResolver().query(missingCallUri,
null, where, null, null);
} catch (SQLiteException e) {
return missCallCount;
}
if (cursorCall != null) {
missCallCount = cursorCall.getCount();
cursorCall.close();
}
return missCallCount;
}
//ADD END
//add by xiong.chen for ...
static Bitmap createIconBitmap(Bitmap b, int count) {
if (count == 0) return b;
int textureWidth = b.getWidth();
final Canvas canvas = sCanvas;
Paint paint = new Paint();
canvas.setBitmap(b);
paint.setColor(Color.RED);
canvas.drawCircle(textureWidth - 17, 16, 16, paint);
paint.setColor(Color.WHITE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
canvas.drawCircle(textureWidth - 17, 16, 16, paint);
Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);
countPaint.setColor(Color.WHITE);
countPaint.setTextSize(26f);
countPaint.setTypeface(Typeface.DEFAULT_BOLD);
float x = textureWidth - 24;
if (count > 9) x -= 4;
canvas.drawText(String.valueOf(count), x, 25, countPaint);
//END
return b;
}
//ADD BY XIONG.CHEN ...
int missCall = getMissCallCount();
int missMms = getMissMmsCount();
if(missCall != 0) {
setMmsOrPhoneNum(PHONE_ICON_NAME, missCall);
}
if(missMms != 0) {
setMmsOrPhoneNum(MMS_ICON_NAME, missMms);
}