我使用 Flow 而不是 LiveData 来收集片段中的数据。在 Fragment AI 中,观察(或者更确切地说收集)我的片段 onViewCreated 中的数据,如下所示:
lifecycleScope.launchWhenStarted {
availableLanguagesFlow.collect {
languagesAdapter.setItems(it.allItems, it.selectedItem)
}
}
Run Code Online (Sandbox Code Playgroud)
问题。然后,当我转到片段 B 然后返回片段 A 时,我的收集函数被调用两次。如果我再次访问片段 B 并返回 A - 那么收集函数将被调用 3 次。等等。
一旦流发出第一个值,就会崩溃。
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object kotlinx.coroutines.flow.FlowCollector.emit(java.lang.Object, kotlin.coroutines.Continuation)' on a null object reference
at kotlinx.coroutines.flow.FlowKt__ErrorsKt$catchImpl$$inlined$collect$1.emit(Collect.kt:136)
at com.happyfleet.app.data.UsbDataSource$mapResponse$$inlined$map$1$2.emit(Collect.kt:137)
at com.happyfleet.app.data.UsbDataSource$mapResponse$$inlined$filter$1$2.emit(Collect.kt:137)
at kotlinx.coroutines.flow.SharedFlowImpl.collect(SharedFlow.kt:351)
at kotlinx.coroutines.flow.SharedFlowImpl$collect$1.invokeSuspend(Unknown Source:15)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:234)
at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:190)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:161)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
at kotlinx.coroutines.flow.SharedFlowImpl.emitSuspend(SharedFlow.kt:472)
at kotlinx.coroutines.flow.SharedFlowImpl.emit(SharedFlow.kt:374)
Run Code Online (Sandbox Code Playgroud)
我的视图模型代码:
@HiltViewModel
class ErrorActivityViewModel @Inject constructor(
private val sendActivityReportUseCase: SendActivityReportUseCase,
private val connectionRepository: ConnectionRepository
) : ViewModel() {
fun sendActivityReport() = viewModelScope.launch {
sendActivityReportUseCase.execute()
}
init {
subscribeToSessionTime()
}
private fun subscribeToSessionTime() = viewModelScope.launch …Run Code Online (Sandbox Code Playgroud) 我正在从firebase>compose notification page发送测试推送通知。我在 Android 8、9、10、11 上收到推送通知,但在 12 上则没有。
我有两台运行 Android 12 的设备:实体手机 Samsung S22 和 Pixel 模拟器。在这两种情况下,推送通知都不会出现在通知托盘中。
笔记:
android:exported="false"- 仍然不起作用我在 android 清单中的服务:
<service
android:name=".old.push.FCMHandlerService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud) push-notification firebase firebase-cloud-messaging android-12
Firebase Cloud Messaging 将推送发送到我的应用程序。当应用程序关闭时,它们会出现在系统托盘中。
任务是当用户单击托盘中的推送通知时打开应用程序并执行某些操作(例如打开另一个片段)。
问题是我不知道当用户单击推送通知时如何覆盖默认行为。是否有某种回调、广播接收器等?
我的 Firebase 消息服务
class FCMHandlerService : FirebaseMessagingService() {
private val intercomPushClient = IntercomPushClient()
override fun onMessageReceived(remoteMessage: RemoteMessage) {
logd("onMessageReceived $remoteMessage")
val notification = remoteMessage.notification ?: return
logd("Remote message body${notification.body} channel id${notification.channelId} message id${remoteMessage.messageId}")
//you can create here your custom notification when the app receives push being foreground
}
override fun onNewToken(token: String) {
super.onNewToken(token)
logd("onNewToken $token")
instance.sendFirebasePushRegistrationToken(token)
}
}
Run Code Online (Sandbox Code Playgroud)
清单中的服务
<service
android:name=".old.push.FCMHandlerService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud) 我必须使用改装2.0.2和xml api响应.但我的自定义xml转换器永远不会被调用.
玩弄这个我发现:
任何人,如何让我的简单的xml转换器被调用?我做错了什么,或改装2.0.2不知何故不支持简单的xml转换器.
我解析响应的java类:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.convert.Convert;
public class PassengerResponse {
@Element
@Convert(value = SomeConverter.class)
private String id;
}
Run Code Online (Sandbox Code Playgroud)
从未调用的自定义xml转换器:
import org.simpleframework.xml.convert.Converter;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.OutputNode;
public class SomeConverter implements Converter<String> {
@Override
public String read(InputNode node) throws Exception {
return null;
}
@Override
public void write(OutputNode node, String value) throws Exception {
}
}
Run Code Online (Sandbox Code Playgroud)
我的改造RestClient:
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;
public class RestClient2 {
private UserApiJSON userPassengerApi;
private static final int TIMEOUT …Run Code Online (Sandbox Code Playgroud) 我使用Kotlin和Dagger 2.问题是,当我在实现Dagger时犯了错误(例如,错过@Inject了类约束器)时,IDE并没有特别显示错误的位置.Insead编译器错误总是一样的:
Execution failed for task ':app:kaptDebugKotlin'. Internal compiler error. See log for more details
有故意的类(评论@Inject):
class LoginPresenter //@Inject
constructor(private val request: LoginRequest)
Run Code Online (Sandbox Code Playgroud)
项目build.gradle文件:
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
模块build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.kravets_a.kotlinanddagger"
minSdkVersion 16
targetSdkVersion …Run Code Online (Sandbox Code Playgroud) 我的应用程序可用于开放式 Beta 测试和封闭式内部测试。即使我使用内部测试链接,它也会将我重定向到 Google Play 上的测试版。
笔记:
android ×6
kotlin ×2
android-12 ×1
beta-testing ×1
coroutine ×1
dagger-2 ×1
firebase ×1
heap-memory ×1
hprof ×1
kapt ×1
retrofit ×1