我正在尝试按如下方式启动前台服务:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel chan = new NotificationChannel(
getApplicationContext().getPackageName(),
"My Foreground Service",
NotificationManager.IMPORTANCE_LOW);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
this, "MyChannelId");
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.my_icon)
.setContentTitle("App is running on foreground")
.setPriority(NotificationManager.IMPORTANCE_LOW)
.setCategory(Notification.CATEGORY_SERVICE)
.setChannelId("MyChannelId")
.build();
startForeground(1, notification);
}
Run Code Online (Sandbox Code Playgroud)
此解决方案适用于 Android 8.0 模拟器。但是我在 Android 8.1、9.0 和 10.0 模拟器上遇到以下错误。
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=MyChannelId pri=2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 …Run Code Online (Sandbox Code Playgroud) 我正在使用两个库,它们依赖于两个不同版本的 Koin。
其中一个库具有org.koin:koin-android:2.0.1 依赖项,另一个库具有io.insert-koin:koin-core-jvm:3.0.1依赖项。
在编译时,我收到了大量的重复类错误,如下所示,
Duplicate class org.koin.android.BuildConfig found in modules jetified-koin-android-2.0.1-runtime (org.koin:koin-android:2.0.1) and jetified-koin-android-3.0.1-runtime (io.insert-koin:koin-android:3.0.1)
Duplicate class org.koin.android.ext.koin.KoinExtKt$androidContext$1 found in modules jetified-koin-android-2.0.1-runtime (org.koin:koin-android:2.0.1) and jetified-koin-android-3.0.1-runtime (io.insert-koin:koin-android:3.0.1)
Duplicate class org.koin.core.scope.Scope$injectOrNull$1 found in modules jetified-koin-core-2.0.1 (org.koin:koin-core:2.0.1) and jetified-koin-core-jvm-3.0.1 (io.insert-koin:koin-core-jvm:3.0.1)
Duplicate class org.koin.java.KoinJavaComponent$inject$1 found in modules jetified-koin-core-jvm-3.0.1 (io.insert-koin:koin-core-jvm:3.0.1) and jetified-koin-java-2.0.1 (org.koin:koin-java:2.0.1)
...
Run Code Online (Sandbox Code Playgroud)
当我尝试排除其中之一时,如下所示,
configurations {
all {
exclude group: "io.insert-koin", module: "koin-android"
exclude group: "io.insert-koin", module: "koin-core-jvm"
}
}
Run Code Online (Sandbox Code Playgroud)
我在相关的运行时遇到了 NoClassDefFoundError 。
当排除org.koin:koin-android:2.0.1时
Caused by: …Run Code Online (Sandbox Code Playgroud)