用RemoteView自定義佈局很簡單的能實現通知欄的圖標自定義。可是因爲項目需求不能用xml佈局,只能用代碼寫。也沒想到怎麼用Java代碼實現RemoteView佈局。然後想到了如下方法:
Class<?> localClass = Class.forName("com.android.internal.R$id");
// int = localClass.getField("title").getInt(localClass);
// n = localClass.getField("text").getInt(localClass);
int iconID = localClass.getField("icon").getInt(localClass);
int icon = mContext.getApplicationInfo().icon;
Intent notificationIntent = new Intent(mContext, KongActivity.class);
Bundle bundle = this.mAdvData.convertToBundle();
notificationIntent.putExtras(bundle);
notificationIntent.putExtra(KongActivity.STRING_ACTION,
AdvAnalysis.ID_NOTIFICATION);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(mContext,
(int) System.currentTimeMillis(), notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification(icon,
this.mAdvData.getAdvTitle(), System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.setLatestEventInfo(mContext,
this.mAdvData.getAdvTitle(), this.mAdvData.getAdvContent(),
contentIntent);
Bitmap iconBitmap = Tools.getLocalBitmap(String_iconPath);
if (iconBitmap != null) {
notification.contentView.setImageViewBitmap(iconID, iconBitmap);
}
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService("notification");
mNotificationManager.notify(this.INT_NOTIFIID, notification);
最主要的就是這個iconID。用這個可以給notification的icon賦值。
還有就是Intent.FLAG_ACTIVITY_CLEAR_TASK。如果主程序爲退出(就是用home退的軟件)的時候點擊通知欄會帶出應用。加上這個Flag可以避免這種情情況。什麼原因我這兒不細說了。可以Google下。