1.フロントデスクを使用しました
権限を追加するには
<アンドロイドの使用許可:名= " android.permission.FOREGROUND_SERVICE " />
通知コードの一部も改正されなければなりません
// アンドロイド8.0以后新增 文字列CHANNEL_ONE_ID = "com.example.servicetest" 。 文字列CHANNEL_ONE_NAME =「チャンネル・ワン」。 NotificationChannel notificationChannel = nullを。 もし(Build.VERSION.SDK_INT> = Build.VERSION_CODES.O){ notificationChannel = 新しいNotificationChannel(CHANNEL_ONE_ID、CHANNEL_ONE_NAME、NotificationManager.IMPORTANCE_HIGH)。 notificationChannel.enableLights(真の); notificationChannel.setLightColor(Color.RED)。 notificationChannel.setShowBadge(真); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC)。 マネージャNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE)。 manager.createNotificationChannel(notificationChannel)。 // 创建前台服务 テントの意図= 新しいテント(これ。、MainActivity クラス)。 PendingIntentのPI = PendingIntent.getActivity(本、0、意図、0 ); 通知通知 = 新しい Notification.Builder(この) .setChannelId(CHANNEL_ONE_ID)// 新增 .setContentTitle( "これはコンテンツのタイトルである" ) .setContentText( "これはコンテンツのタイトルである" ) .setWhen(のSystem.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(PI) .build( ); notification.flags | = Notification.FLAG_NO_CLEAR。// 新增 startForeground(1 、通知)。 }