如何使android前台服务通知在中文ROM中具有粘性?

如何使android前台服务通知在中文ROM中具有粘性?,android,foreground-service,notification-channel,foregroundnotification,Android,Foreground Service,Notification Channel,Foregroundnotification,我试着在后台使用前台服务运行这项服务,除了维梧、oppo、oneplus等中国设备外,它在所有设备上都能正常工作。这些设备会在应用程序从最近列表中删除后立即删除该服务 甚至前台通知也不是粘性的。它可以游泳。然而,我想在它被清除后再次显示通知,有什么办法吗 例如,我在IDM(Internet下载管理器)等应用程序中看到,当下载任何文件时,通知仍然可见。即使被清除,通知也会再次弹出 如何获得这种类型的功能 我的代码: MainActivity.java @Override prot

我试着在后台使用前台服务运行这项服务,除了维梧、oppo、oneplus等中国设备外,它在所有设备上都能正常工作。这些设备会在应用程序从最近列表中删除后立即删除该服务

甚至前台通知也不是粘性的。它可以游泳。然而,我想在它被清除后再次显示通知,有什么办法吗

例如,我在IDM(Internet下载管理器)等应用程序中看到,当下载任何文件时,通知仍然可见。即使被清除,通知也会再次弹出

如何获得这种类型的功能

我的代码:

MainActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startService(View v) {
        Intent serviceIntent = new Intent(this, ExampleService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(serviceIntent);
        } else startService(serviceIntent);
    }

    public void stopService(View v) {
        Intent serviceIntent = new Intent(this, ExampleService.class);
        stopService(serviceIntent);
    }

}
ExampleService.java


    final Handler worker = new Handler();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("Foreground Service", "*********** Service Created ***********");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        worker.removeCallbacksAndMessages(null);
        Log.d("Foreground Service", "*********** Service Destroyed ***********");
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, App.NOTIFICATION_CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText("Working in background")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setAutoCancel(false)
                .setOngoing(true)
                .setContentIntent(pendingIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification.setCategory(Notification.CATEGORY_PROGRESS);
        }
        startForeground(1, notification.build());

        worker.post(new Runnable() {
            @Override
            public void run() {
                Log.i("Info", Thread.currentThread().getName());
                Toast toast = Toast.makeText(getApplicationContext(), "Service Ping", Toast.LENGTH_SHORT);
                toast.show();
                Log.d("Foreground Service", "*********** Service working ***********");
                worker.postDelayed(this, 5000);
            }
        });
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}