首页 > 移动平台 > 详细

Android 9.0版本及以上开发时遇到的一些版本问题

时间:2020-03-01 19:04:23      阅读:221      评论:0      收藏:0      [点我收藏+]

1.使用前台服务

要加上权限

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

同时通知部分的代码也要修改

//android 8.0以后新增
        String CHANNEL_ONE_ID="com.example.servicetest";
        String CHANNEL_ONE_NAME = "Channel One";
        NotificationChannel notificationChannel = null;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(notificationChannel);

            //创建前台服务
            Intent intent = new Intent(this,MainActivity.class);
            PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
            Notification notification = new Notification.Builder(this)
                    .setChannelId(CHANNEL_ONE_ID)//新增
                    .setContentTitle("This is content title")
                    .setContentText("This is content title")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pi)
                    .build();
            notification.flags |= Notification.FLAG_NO_CLEAR;//新增
            startForeground(1,notification);
        }

 

Android 9.0版本及以上开发时遇到的一些版本问题

原文:https://www.cnblogs.com/zsben991126/p/12391269.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!