Android 8.0 适配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BlueSky003/article/details/82971702

Android 8.0 适配:targetSdkVersion >= 26

1、应用图标适配

  1. 郭林大神:https://blog.csdn.net/guolin_blog/article/details/79417483

2、系统通知适配

  1. 郭林大神:https://blog.csdn.net/guolin_blog/article/details/79854070
public class NotificationUtil {

    @RequiresApi(Build.VERSION_CODES.O)
    public static void createChannelId(Context context,String groupId,String groupName , String channelId,String channelName){

        // 分组(可选)groupId 要唯一
        NotificationChannelGroup ncp1 = new NotificationChannelGroup(groupId, groupName);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannelGroup(ncp1);

        // channelId 要唯一
        NotificationChannel chan = new NotificationChannel(channelId,
                channelName,
                NotificationManager.IMPORTANCE_DEFAULT);
        chan.setLightColor(Color.GREEN);
        chan.setGroup(groupId);
        // VISIBILITY_PRIVATE:表面只有当没有锁屏的时候才能够显示,VISIBILITY_PUBLIC:表明任何情况下都会显示,VISIBILITY_SECRET:表明在pin,password等安全锁和没有锁屏的情况下才能够显示
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(chan);
    }

    public static void sendSimpleNotification(Context context) {

        int id = 1000;
        String groupId = "groupId_1";
        String groupName = "渠道组名1";
        String channelId = "channelId_1";
        String channelName = "渠道名1";

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);

        builder.setContentTitle("简单的通知的标题")
                .setContentText("这是通知内容")
                .setLargeIcon(BitmapFactory.decodeResource(
                        context.getResources(),
                        R.mipmap.ic_launcher))
                .setAutoCancel(true)// 自己维护通知的消失
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        //构建一个Intent
        Intent resultIntent = new Intent( context,MainActivity.class);
//        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 封装一个Intent
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                context, 0, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(resultPendingIntent);

        // 这里必须设置chanenelId,要不然该通知在8.0手机上,不能正常显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannelId(context,groupId,groupName,channelId,channelName);
            builder.setChannelId(channelId);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, builder.build());
    }

    public static void sendSimpleNotificationSecond(Context context) {
        int id = 1002;
        String groupId = "groupId_2";
        String groupName = "渠道组名2";
        String channelId = "channelId_2";
        String channelName = "渠道名2";
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle("简单的通知的标题2")
                .setContentText("这是通知内容2")
                .setLargeIcon(BitmapFactory.decodeResource(
                        context.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher);

        //构建一个Intent
        Intent resultIntent = new Intent( context,ResultActivity.class);
//        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 封装一个Intent
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                context, 0, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(resultPendingIntent);

        // 这里必须设置chanenelId,要不然该通知在8.0手机上,不能正常显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannelId(context,groupId,groupName,channelId,channelName);
            builder.setChannelId(channelId);
        }

        //发送通知
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, builder.build());
    }

    //    当Channel已经存在时,后面的createNotificationChannel方法仅能更新其name/description,
    //    以及对importance进行降级,其余配置均无法更新。所以如果有必要的修改只能创建新的渠道,删除旧渠道
    public  static void deleteNotificationChannel(Context context , String channelId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.deleteNotificationChannel(channelId);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/BlueSky003/article/details/82971702