@Override public void onCreate() { super.onCreate(); Log.d("MyService", "onCreate executed"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("fore_service", "前台服务", NotificationManager.IMPORTANCE_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); Intent intentForeSerive = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentForeSerive, 0); Notification notification = new NotificationCompat.Builder(this, "fore_service") .setContentTitle("This is content title") .setContentText("This is content text") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); startForeground(1, notification); } }
第一行代码 第二版 前台服务 Android 8.0开发代码修改
最新推荐文章于 2024-07-25 19:04:18 发布