我是Android初学者,在看第一行代码学习自学时,其中通知用到NotificationCompat()这个V4库中的方法。但是在实际使用时发现书上的代码已经过时并且Android8.0已经不支持这种写法,经过几天的研究终于写出一个可以同时兼容Android8.0及以下的通知写法再此把我的研究过程写给大家。


  ·查找的资料

  代码过时第一想到的就是看源码的替代代码是什么,所以我贴上源码的截图参考


  仔细看一下官方的说明

  通过百度在一篇文章中详细介绍了channel的用法及其特性,我贴出地址 http://www.jianshu.com/p/92afa56aee05
  我按照文章中的方法创建了channel并在NotificationCompat中使用,但是在模拟器中出现了BUG 界面出错并且抽风。

  贴出错误代码

            String id = "channel_1";            String description = "123";            int importance = NotificationManager.IMPORTANCE_HIGH;            NotificationChannel mChannel = new NotificationChannel(id, "123", importance);            //  mChannel.setDescription(description);            //  mChannel.enableLights(true);            //  mChannel.setLightColor(Color.RED);            //  mChannel.enableVibration(true);            //  mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});            mNotificationManager.createNotificationChannel(mChannel);            Notification notification = new Notification.Builder(this, id)                    .setContentTitle("Title")                    .setSmallIcon(R.mipmap.ic_launcher)                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))                    .setContentTitle("您有一条新通知")                    .setContentText("这是一条逗你玩的消息")                    .setAutoCancel(true)//                    .setContentIntent(pintent)                    .build();            mNotificationManager.notify(1, notification);
这样写会出现如下错误还伴随卡死
  大概可能是不安全吧 关于Android O的写法在网上很少 最后在一个国外网站上看到一种写法可行 并且我把它改编 还可以适配Android8.0以下的系统   贴出国外网站 http://forum.yourwebapp.mobi/android-o-how-to-use-notification-channels/不得不说标题非常亲民啊:ANDROID O: HOW TO USE NOTIFICATION CHANNELS

  ·改编后的具有简单兼容性的代码

NotificationUtils:

import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.content.Context;import android.content.ContextWrapper;import android.os.Build;import android.support.v4.app.NotificationCompat;/** * Created by LaoZhao on 2017/11/19. */public class NotificationUtils extends ContextWrapper {    private NotificationManager manager;    public static final String id = "channel_1";    public static final String name = "channel_name_1";    public NotificationUtils(Context context){        super(context);    }    public void createNotificationChannel(){        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);        getManager().createNotificationChannel(channel);    }    private NotificationManager getManager(){        if (manager == null){            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        }        return manager;    }    public Notification.Builder getChannelNotification(String title, String content){        return new Notification.Builder(getApplicationContext(), id)                .setContentTitle(title)                .setContentText(content)                .setSmallIcon(android.R.drawable.stat_notify_more)                .setAutoCancel(true);    }    public NotificationCompat.Builder getNotification_25(String title, String content){        return new NotificationCompat.Builder(getApplicationContext())                .setContentTitle(title)                .setContentText(content)                .setSmallIcon(android.R.drawable.stat_notify_more)                .setAutoCancel(true);    }    public void sendNotification(String title, String content){        if (Build.VERSION.SDK_INT>=26){            createNotificationChannel();            Notification notification = getChannelNotification                    (title, content).build();            getManager().notify(1,notification);        }else{            Notification notification = getNotification_25(title, content).build();            getManager().notify(1,notification);        }    }}

MainActivity:

import android.app.NotificationManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private NotificationManager mManager;    private Button sendn;    public static final String id = "channel_1";    public static final String name = "名字";    private NotificationUtils notificationUtils;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendn = findViewById(R.id.send_notice);        sendn.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.send_notice:                NotificationUtils notificationUtils = new NotificationUtils(this);                notificationUtils.sendNotification("测试标题", "测试内容");        }    }}

布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>    

  经过测试可以在8.0模拟器和7.0模拟器运行。

更多相关文章

  1. Android(安卓)应用验证码模糊效果
  2. Android(安卓)app实现自更新和安装,权限检测适配Android6.0以下和
  3. Android(安卓)代码热修复详解
  4. android滑动切换屏幕(扒的是launcher2/workspace的源码)
  5. 这是一份非常适合收藏的Android进阶/面试重难点整理
  6. KISS Android(安卓)Library - Android库
  7. Android(安卓)怎样获得手机信息
  8. Android学习札记39:关于安全退出已创建多个Activity的应用(2)
  9. 如何把React Native嵌入到原生android应用中

随机推荐

  1. android:shape的使用
  2. android:shape的使用
  3. Android学习笔记之布局2
  4. 系出名门Android(7) - 控件(View)之ZoomC
  5. Android(安卓)3.0 r1 API中文文档(105)
  6. Android(安卓)3.0 r1中文API文档(103) ―
  7. [整] Android(安卓)ListView 去除边缘阴
  8. Android中ProgressBar的使用-通过Handler
  9. 第一章 开始启程,你的第一行Android代码
  10. Android中的多线程