原文首發(fā)地址 Android Launcher 界面添加未接來電 / 未讀短信條數(shù)顯示
本文代碼基于 Android 6.0(高通),原生不支持未接來電以及未讀短信的圖標右上角數(shù)目顯示辆苔。
涉及到幾個文件:
- packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
- packages/apps/Launcher3/src/com/android/launcher3/Utilities.java
- packages/apps/Launcher3/AndroidManifest.xml
具體的思路:Launcher
中注冊 ContentObserver
來監(jiān)聽短信和電話數(shù)據(jù)庫瑟由,當數(shù)據(jù)發(fā)生變化時录别,讀取讀取數(shù)據(jù)庫未讀數(shù)目之后通過 ICON_NAME 丽猬,重新繪制
短信,電話的圖標(在原圖右上角畫圓和數(shù)字)。
具體代碼:
1. Launcher.java
- 首先是定義兩個 ContentObserver
private final static int UPDATE_MMS_ICON = 826;
private final static int UPDATE_CALL_ICON = 1206;
public class SMSContentObserver extends ContentObserver {
private Handler mHandler;
public SMSContentObserver(Context context, Handler handler) {
super(handler);
mHandler = handler;
}
@Override
public void onChange(boolean selfChange) {
Log.i("Launcher-","SMSContentObserver onChange");
mHandler.removeMessages(UPDATE_MMS_ICON);
Message msg = mHandler.obtainMessage(UPDATE_MMS_ICON);
msg.obj = getMissMmsCount();
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) {
Log.i("Launcher-","CallContentObserver onChange");
mHandler.removeMessages(UPDATE_CALL_ICON);
Message msg = mHandler.obtainMessage(UPDATE_CALL_ICON);
msg.obj = getMissCallCount();
mHandler.sendMessage(msg);
}
}
- 在 onCreate() 中注冊 ContentObserver
private SMSContentObserver smsContentObserver = null;
private CallContentObserver callContentObserver = null;
smsContentObserver = new SMSContentObserver(this,mHandler);
callContentObserver =new CallContentObserver(this,mHandler);
getContentResolver().registerContentObserver(Calls.CONTENT_URI,true,callContentObserver);
getContentResolver().registerContentObserver(Uri.parse("content://mms-sms/"),true,smsContentObserver);
- 定義兩個 ICON_NAME
// 這兩個 ICON_NAME 根據(jù)自己實際系統(tǒng)短信和電話頁面對應包名填寫
private final static String PHONE_ICON_NAME = "com.android.dialer.DialtactsActivity";
private final static String MMS_ICON_NAME = "com.android.messaging.ui.conversationlist.ConversationListActivity";
- mHandler 中處理
@Thunk
final Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.i("Launcher-","mHandler msg.what = " + msg.what);
if (msg.what == ADVANCE_MSG) {
int i = 0;
for (View key : mWidgetsToAdvance.keySet()) {
final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
final int delay = mAdvanceStagger * i;
if (v instanceof Advanceable) {
mHandler.postDelayed(new Runnable() {
public void run() {
((Advanceable) v).advance();
}
}, delay);
}
i++;
}
sendAdvanceMessage(mAdvanceInterval);
}
//ADD BY Bruce Yang 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());
}
return true;
}
});
- 獲取數(shù)據(jù)庫中未讀數(shù)目
private int getMissMmsCount() {
Log.i("Launcher-","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();
}
Log.i("Launcher-","getMissMmsCount missSmsCount = " + missSmsCount);
return missSmsCount;
}
private int getMissCallCount() {
Log.i("Launcher-","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();
}
Log.i("Launcher-","getMissCallCount missCallCount = " + missCallCount);
return missCallCount;
}
- 設置 ICON 未讀數(shù)
/**
*
* @param flag 更新電話或短信 ICON
* @param missCount 未讀數(shù)
*/
private void setMmsOrPhoneNum(final String flag, final int missCount) {
Log.i("Launcher-","flag = "+flag +" missCount = "+missCount);
if(mWorkspace == null) return;
ArrayList<CellLayout> 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);
}
}
}
}
}
});
}
}
- 第一次啟動 Launcher 就能獲取未讀數(shù)目凯正,在 finishBindingItems() 添加邏輯
//ADD BY Bruce Yang
int missCall = getMissCallCount();
int missMms = getMissMmsCount();
if(missCall != 0) {
setMmsOrPhoneNum(PHONE_ICON_NAME, missCall);
}
if(missMms != 0) {
setMmsOrPhoneNum(MMS_ICON_NAME, missMms);
}
- 在 onDestroy() 中反注冊 ContentObserver
getContentResolver().unregisterContentObserver(smsContentObserver);
getContentResolver().unregisterContentObserver(callContentObserver);
2. Utilities.java
新增構(gòu)造方法,用于重新繪制帶數(shù)字的應用圖標豌蟋。
//add by Bruce Yang for ...
static Bitmap createIconBitmap(Bitmap b, int count) {
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888,true);
Log.i("Launcher-","b.isMutable() = "+b.isMutable()); // 如果為 false 就會拋出 java.lang.IllegalStateException 異常廊散, http://bbs.csdn.net/topics/370021698
if (count == 0) return b;
int textureWidth = bitmap.getWidth();
final Canvas canvas = sCanvas;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
canvas.setBitmap(bitmap);
paint.setColor(Color.RED);
canvas.drawCircle(textureWidth - 17-6, 16+6, 16+6, paint);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
canvas.drawCircle(textureWidth - 17-6, 16+6, 16+6, 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-4;
if (count > 9) x -= 4+6;
if (count > 99) {
countPaint.setTextSize(22f);
String text = String.valueOf(99) + "+";
canvas.drawText(text, x-2, 25+5, countPaint);
} else {
String text = String.valueOf(count);
canvas.drawText(text,x, 25+5, countPaint);
}
return bitmap;
}
3. AndroidManifest.xml
添加如下兩個權(quán)限,由于 Android 6.0 以上需要動態(tài)權(quán)限申請梧疲,這里為了直接獲取權(quán)限允睹,可以將 targetSdkVersion 改成 21(原來是 23)。
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
4. 修改對比
修改前:
修改后:
參考文章:
http://blog.csdn.net/chenxiong668/article/details/12851357
http://blog.csdn.net/kerancsdn/article/details/26705767