小编Mat*_* H.的帖子

在S5 Neo上,AlarmManager在睡眠模式下无法正常工作

我在服务中使用AlarmManager每分钟触发一次.

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0,
            getUpdateServiceIntent(mContext), PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Cancel any pending Intent
    am.cancel(pendingIntent);

    // Set a new one
    am.set(AlarmManager.RTC_WAKEUP, 60000, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

在Samsung S5 Neo上:当屏幕处于活动状态时,它正在按预期工作.屏幕关闭时,每5分钟触发一次(而不是一次).

我在S5 Mini(Android 4.4),Nexus 5 5.1和Nexus 5 6.0上尝试了完全相同的代码,这段代码运行正常.

targetSdkVersion是19.

知道如何在屏幕关闭时保持AlarmManager正常工作吗?延迟仍然是5分钟,即使我要求30秒.

编辑:我也尝试了'setExact'方法,但它没有改变任何东西.每次报警之间仍有5分钟的间隔.

android alarmmanager samsung-mobile

8
推荐指数
1
解决办法
4338
查看次数

如何使用Koin管理用户范围?

我正在尝试使用Koin创建用户范围。记录用户时,我正在创建范围:

val scope = getKoin().createScope("USER_SCOPE")
Run Code Online (Sandbox Code Playgroud)

当用户单击注销时,我正在破坏范围

    scope?.let {userScope ->
        userScope.close()
        getKoin().deleteScope(userScope.id)
    }
Run Code Online (Sandbox Code Playgroud)

在我的koin模块中,我有一个scopedUserRepository,它只能在用户会话期间使用。我也有使用此存储库的ViewModels和Use Cases,我尝试将存储库注入scoped其中

val appModule = module {
    scoped<UserRepository> { UserDataRepository() }
    viewModel { UserViewModel(getScope("USER_SCOPE").get()) }
    factory { MyUseCase(getScope("USER_SCOPE").get()) }
}
Run Code Online (Sandbox Code Playgroud)

第一次登录时,它可以正常工作,我在视图模型和用例中注入了用户存储库。但是在注销(删除作用域)之后和再次登录后,UserRepository实例仍然完全相同。

我会在示波器用法中错过某些东西吗?

android kotlin koin

7
推荐指数
1
解决办法
366
查看次数

使用 Compose ConstraintLayout 时如何处理丢失/“消失”的视图

我正在尝试使用 Jetpack Compose ConstraintLayout,如果所有视图都可见,则说明效果很好。但如果缺少其中一个视图,沙堡就会倒塌。

例如,如果视图是可选的,我会这样管理它:

val (text1, text2) = createRefs()

ConstraintLayout {
    if (myTextStr.isNotEmpty()) {
        Text(
            text = myTextStr,
            modifier = Modifier
                .constrainAs(text1) {
                    start.linkTo(parent.start)
                    bottom.linkTo(parent.bottom)
                })
    }
    Text(
        text = myTextStr2,
        modifier = Modifier
            .constrainAs(text2) {
                start.linkTo(parent.start)
                bottom.linkTo(text1.top)
            })
}
Run Code Online (Sandbox Code Playgroud)

但是,如果第一个 Text 元素丢失,则所有布局都会被破坏,因为第二个 Text 位置取决于它。

一种可能性是保留文本视图,但0.dp如果 myTextStr 为 null 或为空,则将高度设置为。但我想确保 Compose ConstraintLayout 没有提供更干净的方法来实现此目的

android android-constraintlayout android-jetpack-compose

6
推荐指数
1
解决办法
4111
查看次数

使用RXJava2 Flowable过滤数据

我正在使用Room和RxJava,我想使用第二个功能来过滤来自第一个的数据.

假设房间正在返回用户.

Flowable<List<User> getUsers()
Run Code Online (Sandbox Code Playgroud)

然后我想按年龄> 18过滤用户,所以我执行了以下操作:

userDao.getUsers()
.flatMap(listUser -> Flowable.fromIterable(listUser).filter(user -> user.age > 18))
.toList()
.toFlowable()
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用.我的猜测是toList()永远不会完成,因为onTerminated永远不会被房间调用.所以我的问题是:我做错了什么?如何过滤我的用户并在最后仍然拥有Flowable?

谢谢

android rx-java2 android-room

3
推荐指数
1
解决办法
1481
查看次数