我可以从Android NotificationListenerService打开Room数据库吗?
当我尝试在NotificationListenerService中打开数据库时,我的应用程序崩溃,说
time_t timer
这些错误是什么意思?
如果我删除inMemoryDatabaseBuilder行,它将正常工作。我最初是从一个普通的databaseBuilder开始的,但是我正在尝试使用内存中的一个,以防万一是问题所在(看来不是问题所在)。
如果我只是W/ActivityManager: Force finishing activity com.xyz.abc.notificationmanager/.MainActivity
W/InputDispatcher: channel '79ec910 com.xyz.abc.notificationmanager/com.xyz.abc.notificationmanager.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
E/InputDispatcher: channel '79ec910 com.xyz.abc.notificationmanager/com.xyz.abc.notificationmanager.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
而不用val db = Room.inMemoryDatabaseBuilder(applicationContext, TheDatabase::class.java)
,它不会崩溃。
代码:
数据库:
.build()
NotificationListenerService:
package com.xyz.abc.notificationmanager
import android.arch.persistence.room.*
@Entity(tableName="matches")
class SettingsMatch {
@PrimaryKey(autoGenerate=true)
var id: Long = 0
@ColumnInfo(name="package_name")
var packageName: String? = null
@ColumnInfo(name="group_id")
var groupId: Long? = null
@ColumnInfo(name="comment")
var comment: String? = null
@ColumnInfo(name="search_title")
var searchTitle: String? = null
@ColumnInfo(name="search_content")
var searchContent: String? = null
}
@Entity(tableName="groups", foreignKeys=arrayOf(
ForeignKey(entity=SettingsMatch::class, parentColumns=arrayOf("id"), childColumns=arrayOf("group_id"))
))
class SettingsGroup {
@PrimaryKey(autoGenerate=true)
var id: Long = 0
@ColumnInfo(name="comment")
var comment: String? = null
@ColumnInfo(name="min_secs_between_alerts")
var minSecsBetweenAlerts: Int = 0
@ColumnInfo(name="last_alert_time")
var lastAlertTime: Int = 0
@ColumnInfo(name="alert_when_screen_on")
var alertWhenScreenOn: Boolean = true
@ColumnInfo(name="volume_percent")
var volumePercent: Int = 100
@ColumnInfo(name="vibration_pattern")
var vibrationPattern: LongArray = longArrayOf(0)
}
@Dao
interface SettingsMatchDao {
@get:Query("SELECT * FROM matches")
val all: List<SettingsMatch>
@Query("SELECT * FROM matches WHERE package_name=:packageName")
fun loadAllByPackageName(packageName: String): List<SettingsMatch>
@Query("SELECT * FROM matches "
+ "WHERE package_name=:packageName AND "
+ "(:searchTitle LIKE CONCAT('%', search_title, '%') OR :searchContent LIKE CONCAT('%', search_content, '%')) "
+ "LIMIT 1")
fun loadByPackageNameTitleContent(packageName: String, searchTitle: String, searchContent: String): SettingsMatch
@Insert
fun insert(settingsMatch: SettingsMatch): Long
@Delete
fun delete(settingsMatch: SettingsMatch)
}
@Dao
interface SettingsGroupDao {
@get:Query("SELECT * FROM groups")
val all: List<SettingsGroup>
@Query("SELECT * FROM groups WHERE id=:id LIMIT 1")
fun loadByGroupId(id: Int): SettingsGroup
@Query("SELECT * FROM groups "
+ "INNER JOIN matches ON matches.group_id = groups.id "
+ "WHERE package_name=:packageName AND "
+ "(:searchTitle LIKE CONCAT('%', search_title, '%') OR :searchContent LIKE CONCAT('%', search_content, '%')) "
+ "LIMIT 1")
fun loadByPackageNameTitleContent(packageName: String, searchTitle: String, searchContent: String): SettingsGroup
@Insert
fun insert(settingsGroup: SettingsGroup): Long
@Delete
fun delete(settingsGroup: SettingsGroup)
}
@Database(entities=arrayOf(SettingsMatch::class, SettingsGroup::class), version=1)
abstract class TheDatabase : RoomDatabase() {
abstract fun matchDao(): SettingsMatchDao
abstract fun groupDao(): SettingsGroupDao
}