在无障碍服务类中调用MyForegroundService前台服务,但是设置里的允许通知都是灰色无法点击的
serviceIntent = new Intent(this, MyForegroundService.class);
ContextCompat.startForegroundService(this, serviceIntent);
public class MyForegroundService extends Service {
private static final String CHANNEL_ID = "my_service_channel";
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("kktool", "MyForegroundService__onStartCommand: ");
// // 创建通知
// Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
// .setContentTitle("My Service")
// .setContentText("Running...")
// .setSmallIcon(R.mipmap.ic_launcher)
// .build();
//
// // 启动前台服务
// startForeground(1, notification);
//
// return START_STICKY;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 创建一个 NotificationChannel
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"My Service Channel", NotificationManager.IMPORTANCE_DEFAULT);
// 自定义设置通知声音、震动等
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200});
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
// 构建一个 Notification
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Some Service")
.setContentText("服务正在运行...")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
// 启动前台服务
// 通知栏标识符 前台进程对象唯一SERVICE_ID
startForeground(1, notification);
} else {
startService(intent); // API < 26 直接启动服务即可
}
return START_STICKY;
}
//....
MyForegroundService__onStartCommand有输出,但是就是没反应,什么反应都没有