美文网首页
Android系统悬浮提示框

Android系统悬浮提示框

作者: 坑逼的严 | 来源:发表于2021-08-10 16:48 被阅读0次
1628584340599.gif

点击按钮的代码

if(!isPermissionOpen(WebViewActivity.this)){
                getHangUpPermission(CHANNEL_ID);
            }else{
                Random random = new Random();
                int ids = random.nextInt(Integer.MAX_VALUE);//生成随机id
                NotificationCompat.Builder builder = createForegroundNotification();
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(ids,builder.build());
            }

判断是否开启了横幅提示

public boolean isPermissionOpen(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = NotificationManagerCompat.from(context).getNotificationChannel(CHANNEL_ID);
            if(notificationChannel == null){
                Log.d("yanjin","importance = null");
                return false;
            }else{
                int importance = notificationChannel.getImportance();
                Log.d("yanjin","importance = "+importance);
                return importance != NotificationManager.IMPORTANCE_DEFAULT;
            }
        }
        return NotificationManagerCompat.from(context).areNotificationsEnabled();
    }

进入设置页面开启横幅提示

/**跳转横幅通知权限,详细channelId授予权限*/
    private void getHangUpPermission(String channelId) {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android8.0单个channelid设置
            intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
            intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
        } else {
            // android 5.0以上一起设置
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.putExtra("app_package", getPackageName());
            intent.putExtra("app_uid", getApplicationInfo().uid);
        }
        startActivity(intent);
    }

创建通知并返回,有权限再notify

private NotificationCompat.Builder createForegroundNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 唯一的通知通道的id.

        // Android8.0以上的系统,新建消息通道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //用户可见的通道名称
            String channelName = "Foreground Service Notification";
            //通道的重要程度
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName, importance);
            notificationChannel.setDescription("Channel description");
            //LED灯
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            //震动
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            // 声音(没有声音)
            notificationChannel.setSound(null, null);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        builder.setCategory(Notification.CATEGORY_RECOMMENDATION);
        //通知小图标
        builder.setSmallIcon(R.drawable.ic_launcher_foreground);
        //通知标题
        builder.setContentTitle("NotificationTitle");
        //通知内容
        builder.setContentText("ContentText");
        //设定通知显示的时间
        builder.setWhen(System.currentTimeMillis());
        //设定启动的内容
//        Intent activityIntent = new Intent(this, ForegroundSerActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//        builder.setContentIntent(pendingIntent);
        // sdk5.0以上使用
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
//        builder.setFullScreenIntent(pendingIntent, false);

        //创建通知并返回
        return builder;
    }

相关文章

网友评论

      本文标题:Android系统悬浮提示框

      本文链接:https://www.haomeiwen.com/subject/sapxbltx.html