zoukankan      html  css  js  c++  java
  • notification的通知代码

    public Notification createNotification() {
    String channelId = createNotificationChannel("my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
    RemoteViews notificationLayout = buildStyleOneRemoteViews();
    RemoteViews notificationLayoutExpanded = buildStyleOneRemoteViews1();
    notification = new NotificationCompat.Builder(this, channelId)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setCustomBigContentView(notificationLayoutExpanded)
    .setWhen(cureentTime)
    .setShowWhen(true)
    .setOngoing(true)
    .setSound(null)
    .setVibrate(null)
    .setStyle(null)
    .setPriority(Notification.PRIORITY_HIGH)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.notification_layout_icon)
    .setGroup("group_growth_notification")
    .setGroupSummary(false)
    .setCustomContentView(notificationLayout)
    .setCustomBigContentView(notificationLayoutExpanded)
    .setVisibility(NotificationCompat.VISIBILITY_SECRET);
    notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(mNitificationCount, notification.build());
    return null;
    }

    private String createNotificationChannel(String channelID, String channelNAME, int level) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
    manager.createNotificationChannel(channel);
    return channelID;
    } else {
    return null;
    }
    }


    private RemoteViews buildStyleOneRemoteViews() {
    RemoteViews view = new RemoteViews(getPackageName(), R.layout
    .activity_main1);

    int textColor = getNotificationColor(this);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
    textColor = getNotificationColor(this);
    view.setTextColor(R.id.notification_title, textColor);
    view.setTextColor(R.id.notification_name, textColor);
    view.setTextColor(R.id.notfication_kwai_name, textColor);
    }




    private static final String NOTIFICATION_TITLE = "notification_title";
    public final int INVALID_COLOR = -1; // 无效颜色
    private int notificationTitleColor = INVALID_COLOR; // 获取到的颜色缓存
    /**
    * 获取系统通知栏主标题颜色,根据Activity继承自AppCompatActivity或FragmentActivity采取不同策略。
    *
    * @param context 上下文环境
    * @return 系统主标题颜色
    */
    public int getNotificationColor(Context context) {
    try {
    if (notificationTitleColor == INVALID_COLOR) {
    if (context instanceof AppCompatActivity) {
    notificationTitleColor = getNotificationColorCompat(context);
    } else {
    notificationTitleColor = getNotificationColorInternal(context);
    }
    }
    } catch (Exception ignored) {
    }
    return notificationTitleColor;
    }
    /**
    * 通过一个空的Notification拿到Notification.contentView,通过{@link RemoteViews#apply(Context, ViewGroup)}方法返回通知栏消息根布局实例。
    *
    * @param context 上下文
    * @return 系统主标题颜色
    */
    private int getNotificationColorInternal(Context context) {
    Notification.Builder builder = new Notification.Builder(context);
    builder.setContentTitle(NOTIFICATION_TITLE);
    Notification notification = builder.build();
    try {
    ViewGroup root = (ViewGroup) notification.contentView.apply(context, new FrameLayout(context));
    TextView titleView = (TextView) root.findViewById(android.R.id.title);
    if (null == titleView) {
    iteratorView(root, new Filter() {
    @Override
    public void filter(View view) {
    if (view instanceof TextView) {
    TextView textView = (TextView) view;
    if (NOTIFICATION_TITLE.equals(textView.getText().toString())) {
    notificationTitleColor = textView.getCurrentTextColor();
    }
    }
    }
    });
    return notificationTitleColor;
    } else {
    return titleView.getCurrentTextColor();
    }
    } catch (Exception e) {
    return getNotificationColorCompat(context);
    }
    }
    /**
    * 使用getNotificationColorInternal()方法,Activity不能继承自AppCompatActivity(实测5.0以下机型可以,5.0及以上机型不行),
    * 大致的原因是默认通知布局文件中的ImageView(largeIcon和smallIcon)被替换成了AppCompatImageView,
    * 而在5.0及以上系统中,AppCompatImageView的setBackgroundResource(int)未被标记为RemotableViewMethod,导致apply时抛异常。
    *
    * @param context 上下文
    * @return 系统主标题颜色
    */
    private int getNotificationColorCompat(Context context) {
    try {
    Notification.Builder builder = new Notification.Builder(context);
    Notification notification = builder.build();
    int layoutId = notification.contentView.getLayoutId();
    ViewGroup root = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null);
    TextView titleView = (TextView) root.findViewById(android.R.id.title);
    if (null == titleView) {
    return getTitleColorIteratorCompat(root);
    } else {
    return titleView.getCurrentTextColor();
    }
    } catch (Exception e) {
    }
    return INVALID_COLOR;
    }
    private void iteratorView(View view, Filter filter) {
    if (view == null || filter == null) {
    return;
    }
    filter.filter(view);
    if (view instanceof ViewGroup) {
    ViewGroup viewGroup = (ViewGroup) view;
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
    View child = viewGroup.getChildAt(i);
    iteratorView(child, filter);
    }
    }
    }
    private int getTitleColorIteratorCompat(View view) {
    if (view == null) {
    return INVALID_COLOR;
    }
    List<TextView> textViews = getAllTextViews(view);
    int maxTextSizeIndex = findMaxTextSizeIndex(textViews);
    if (maxTextSizeIndex != Integer.MIN_VALUE) {
    return textViews.get(maxTextSizeIndex).getCurrentTextColor();
    }
    return INVALID_COLOR;
    }
    private int findMaxTextSizeIndex(List<TextView> textViews) {
    float max = Integer.MIN_VALUE;
    int maxIndex = Integer.MIN_VALUE;
    int index = 0;
    for (TextView textView : textViews) {
    if (max < textView.getTextSize()) {
    // 找到字号最大的字体,默认把它设置为主标题字号大小
    max = textView.getTextSize();
    maxIndex = index;
    }
    index++;
    }
    return maxIndex;
    }
    /**
    * 实现遍历View树中的TextView,返回包含TextView的集合。
    *
    * @param root 根节点
    * @return 包含TextView的集合
    */
    private List<TextView> getAllTextViews(View root) {
    final List<TextView> textViews = new ArrayList<>();
    iteratorView(root, new Filter() {
    @Override
    public void filter(View view) {
    if (view instanceof TextView) {
    textViews.add((TextView) view);
    }
    }
    });
    return textViews;
    }
    private interface Filter {
    void filter(View view);
    }



  • 相关阅读:
    一个ip对应多个域名多个ssl证书配置-Nginx实现多域名证书HTTPS,NGINX支持多个带SSL证书的网站同时部署在同一台服务器上
    SVN报错:Node remains in conflict显示冲突的解决办法
    阿里云上部署了zabbix,突然无法收到报警邮件的解决办法
    npm安装socket.io时报错的解决方法(npm WARN enoent ENOENT: no such file or directory, open '/usr/local/nodejs/bin/package.json')
    winscp以命令行方式同步服务器数据到PC机磁盘上
    在阿里云上无法使用mailx发送邮件的解决办法,验证可用。
    编译geth报错的解决方法 make: *** [geth] 错误 1
    ZABBIX 3.0 监控MongoDB性能【OK】
    print命令
    软件开发规范
  • 原文地址:https://www.cnblogs.com/liunx1109/p/14334036.html
Copyright © 2011-2022 走看看