小编Bin*_*ink的帖子

以编程方式禁用/延迟Android锁定屏幕

我想以disable/delay编程方式锁定屏幕.在调查这个,KeyguardManager折旧/不一致,FLAG_DISMISS_KEYGUARD不是一个选项,因为我想在后台执行此操作,我不想因为电池原因使用WakeLock,设备管理似乎没有能力并且,虽然SCREEN_OFF_TIMEOUT可以控制屏幕何时休眠,但我无法找到屏幕锁定的类似超时.

我在这里错过了什么?谢谢.

android

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

双向数据绑定、RecyclerView、ViewModel、Room、LiveData、Oh My

Android 开发新手,我\xe2\x80\x99m 试图将我的注意力集中在与 RecyclerView、ViewModel、Room 和 LiveData 结合的双向数据绑定上。我理解单向绑定,但不能\xe2\x80\x99t 找出双向。

\n\n

简而言之,我\xe2\x80\x99d 希望能够点击 id/switch_enabled 开关并更新 Db 以反映这一点(然后我计划利用它来更新类/Db 中的其他成员)。我认为我需要一些帮助来设置 ViewModel 上的 set(value) 并在 Db 中更新正确的 RecyclerView 项目,但我\xe2\x80\x99m 不确定如何执行此操作,或者这是否是正确或最佳的方法。

\n\n

谢谢。

\n\n

班级:

\n\n
data class Person (@ColumnInfo(name = "first_name") val firstName: String,\n                   @ColumnInfo(name = "last_name") val lastName: String,\n\n                   //...\n\n                   val enabled: Boolean = true\n){\n    @PrimaryKey(autoGenerate = true)\n    var id: Long = 0\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

RecyclerView 的布局详细信息:

\n\n
<layout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    xmlns:tools="http://schemas.android.com/tools">\n\n    <data>\n        <variable\n            name="p" type="com.example.data.Person" />\n    </data>\n\n    <androidx.constraintlayout.widget.ConstraintLayout\n        android:id="@+id/constraintLayout"\n        android:layout_width="match_parent"\n        android:layout_height="wrap_content">\n\n        <TextView\n            android:id="@+id/first_name"\n            android:layout_width="wrap_content"\n …
Run Code Online (Sandbox Code Playgroud)

android-recyclerview android-databinding android-room android-livedata android-viewmodel

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

脉冲音频使用过多的CPU

我正在运行带有 gnome 的 Centos 7 桌面,尽管 Arch 和其他发行版经常出现类似的问题。

当我通过 top 检查我的 cpu 使用情况时,pulseaudio 使用了近 20% 的 cpu。

我四处寻找解决方案,但有很多不正确的解决方案,因此我在这里发布业力点以帮助其他人。

firefox cpu-usage pulseaudio espeak centos7

8
推荐指数
2
解决办法
5827
查看次数

Android 首选项数据存储与现有房间实现

我\xe2\x80\x99m 是Android 开发新手,我\xe2\x80\x99m 即将为我的应用程序实现简单的首选项。看起来 SharedPreferences 是一个死胡同,并且有很多缺点,所以我\xe2\x80\x99m 看看 DataStore(非 Proto)与 Room。 由于我已经在我的应用程序中大量使用 Room 和 LiveData(是的,我知道 Flow 是新热门)来处理其他事情,那么使用 DataStore 也有什么好处吗? 我知道 Room 被推荐用于大型或复杂的数据,因为我\xe2\x80\x99已经审查了以下内容,但我\xe2\x80\x99m希望更有经验的开发人员可以为我进一步击中这个家:

\n

https://android-developers.googleblog.com/2020/09/prefer-storing-data-with-jetpack.html

\n

https://proandroiddev.com/lets-explore-jetpack-datastore-in-android-621f3564b57

\n

https://medium.com/better-programming/jetpack-datastore-improved-data-storage-system-adec129b6e48

\n

谢谢。

\n

android datastore android-preferences android-room

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

android.intent.action.MY_PACKAGE_REPLACED 不工作

我似乎无法理解这一点,也看不到它在 Logcat 中发送。通过审查未收到 ACTION_MY_PACKAGE_REPLACED,看来 MY_PACKAGE_REPLACED 应该是我对 API22 所需的全部。

我缺少什么?

谢谢。

AndroidManifest.xml 中的片段

    <receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

引导接收器

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.BOOT_COMPLETED" ||
                intent.action == "android.intent.action.MY_PACKAGE_REPLACED") {
            Log.d(TAG, "Caught BOOT_COMPLETED or PACKAGE_REPLACED action!")
            GlobalScope.launch(Dispatchers.IO) {
                ...
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

android intentfilter broadcastreceiver android-intent kotlin

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

StrictMode 政策违规:我的应用程序中的 android.os.strictmode.LeakedClosableViolation?

Android 开发新手并第一次在我的应用程序上尝试 StrictMode。我注意到以下内容,并想知道这是否是我的应用程序或库中的问题 - 我不太清楚。

谢谢!

D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: readFromParcel
    at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1929)
    at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:305)
    at android.view.SurfaceControl.finalize(SurfaceControl.java:1104)
    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
    at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
    at java.lang.Daemons$Daemon.run(Daemons.java:139)
    at java.lang.Thread.run(Thread.java:923)
Run Code Online (Sandbox Code Playgroud)

android android-strictmode

6
推荐指数
0
解决办法
967
查看次数

ViewModel、LiveData 和 Transformations.map

Kotlin 和 Android 开发及相关领域的新手,我不确定如何应用某些业务逻辑并转换值。我有一个类的列表,我想修改类中的一个值,同时不影响类中的其他所有内容。一旦我进入视图模型,我不确定如何访问我的类中的时间值来修改它。如果有人能指出我正确的方向,我将不胜感激。

实体与道

import org.threeten.bp.Instant

data class ActionDetails(val time: Instant,
                     val firstName: String,
                     ... )

@Query("SELECT time, first_name as firstName...")
fun liveStatus(): LiveData<List<ActionDetails>>
Run Code Online (Sandbox Code Playgroud)

视图模型

class MainViewModel(private val repository: DataRepository) : ViewModel() {

    private val _actions: LiveData<List<ActionDetails>>
    val actions: LiveData<List<ActionDetails>>
        get() = _actions

    init {
        _actions = Transformations.map(repository.liveStatus()) {
            //Convert Instant value per business rules and convert to formatted string
            time -> ...

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

android kotlin android-livedata android-viewmodel

5
推荐指数
1
解决办法
3232
查看次数