使用 Android 自定义通知Notification时遇到的问题

来源:互联网 发布:网络拓扑结构及配置 编辑:程序博客网 时间:2024/04/29 15:09

在Android的开发过程中难免要用到通知栏,来提醒用户。如果简单的使用系统的方法

NotificationManager mNotifymgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);//使用系统方法获得通知管理者

NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
builder.setTicker(ticker);
builder.setContentTitle(title);//标题
builder.setContentText(content);//内容
mNotifyMgr.notify(NOTIFICATIONS_ID,builder.builder());//添加
但是系统的有时候功能不够用这时我们就要自定义通知栏
RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.view_notification);//new一个通知栏的布局
views.setTextViewText("新的通知栏");
views.setInt(R.id.no_title, "setTextColor", isDarkNotificationTheme(context,id) == true?Color.WHITE:Color.BLACK);//用反射设置颜色

NotificationCompat.Builder builder=new NotificationCompat.Builder(context);//构造一个builder
builder.setContent(views);//把views加进去
builder.setSmallIcon(R.mipmap.ic_launcher);//图标
builder.setTicker(context.getResources().getString(name);
mNotifyMgr.notify(NOTIFICATIONS_ID,builder.builder());
在使用时因为系统机型的原因会出现颜色适配的问题用一下方法可以解决。我在使用时发现一直抱空指针看log发现空指针抱在
 int layoutId =  notification.contentView.getLayoutId();这里,反复检查我发现这里并没有写错,当时苦恼了好久,这个bug还不是所有的手机都有,所以找到时候比较麻烦,后来我仔细查看代码发现以前写代码的兄弟,用一个集合保存了builder,但是在获取颜色时却并没有从集合取,而是新new的所以找不到(我认为是这样),后来改成从集合取果然好了,方法没错,有时候接手代码一定要仔细浏览啊
private static boolean isDarkNotificationTheme(Context context,int id) {
return !isSimilarColor(Color.BLACK, getNotificationColor(context,id));
/**
}
* 获取通知栏颜色
* @param context
* @return
viewGroup.findViewById(android.R.id.title)在一些机型上会为空,所以我们加判断为便获取系统默认的文本区域
*/private static int getNotificationColor(Context context,int id) { Notification notification=builder.build(); Notification notification=builder.build(); int layoutId = notification.contentView.getLayoutId(); ViewGroup viewGroup= (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null, false); if (viewGroup.findViewById(android.R.id.title)!=null) {
return ((TextView) viewGroup.findViewById(android.R.id.title)).getCurrentTextColor(); } return findColor(viewGroup);}private static int findColor(ViewGroup viewGroupSource) { int color=Color.TRANSPARENT; LinkedList<ViewGroup> viewGroups=new LinkedList<>(); viewGroups.add(viewGroupSource); while (viewGroups.size()>0) { ViewGroup viewGroup1=viewGroups.getFirst(); for (int i = 0; i < viewGroup1.getChildCount(); i++) { if (viewGroup1.getChildAt(i) instanceof ViewGroup) { viewGroups.add((ViewGroup) viewGroup1.getChildAt(i)); } else if (viewGroup1.getChildAt(i) instanceof TextView) { if (((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor()!=-1) { color=((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor(); } } } viewGroups.remove(viewGroup1); } return color;}private static boolean isSimilarColor(int baseColor, int color) { int simpleBaseColor=baseColor|0xff000000; int simpleColor=color|0xff000000; int baseRed=Color.red(simpleBaseColor)-Color.red(simpleColor); int baseGreen=Color.green(simpleBaseColor)-Color.green(simpleColor); int baseBlue=Color.blue(simpleBaseColor)-Color.blue(simpleColor); double value=Math.sqrt(baseRed*baseRed+baseGreen*baseGreen+baseBlue*baseBlue); if (value<180.0) { return true; } return false;}



阅读全文
0 0
原创粉丝点击