Tag:

服务不写入SharedPreferences

所以,我有一个在后台处理播放音频文件的服务。 我想能够将服务写入SharedPreferences以便可以根据服务的当前状态更新UI,比如当应用程序从前台 – >后台 – >前台再次运行时。 不幸的是,从服务写入SharedPreferences似乎是不可能的。 我真的不想使用任何静态变量,所以我认为当发生状态变化时,将服务的状态写入后台将是最佳的。 这里是SharedPreferences代码,它真的没什么新意。 private fun saveAudioServiceStateToSharedPreferences(isRunning: Boolean) { d { "saving state: [${isRunning}]" } val sharedPref = PreferenceManager.getDefaultSharedPreferences(this) val editor = sharedPref.edit() editor.putBoolean(getString(R.string.preference_is_audio_service_running), isRunning) editor.commit() } fun isAudioServiceRunning(context: Context): Boolean { val sharedPref = PreferenceManager.getDefaultSharedPreferences(context) val isRunning = sharedPref.getBoolean(context.getString(R.string.preference_is_audio_service_running), false) if (!isRunning) { d { "Audio service is NOT […]

我如何使用Android的DropDownPreference?

我正在尝试使用DropDownPreference构建一个首选项屏幕。 最初我在我的gradle文件中使用了下面的代码compile 'com.android.support:preference-v14:25.3.1'但是当我注意到DropDownPreference时切换到compile 'com.android.support:preference-v7:25.3.1'包含在v7中,而不是v14(我认为v14也可能包括v7中的所有内容,但我猜不是?)。 我的XML看起来像这样: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="@string/pref_title" android:layout_height="match_parent" android:layout_width="match_parent"> <PreferenceCategory android:key="pref_video" android:title="@string/pref_video_title"> <android.support.v7.preference.DropDownPreference android:key="pref_video_quality" android:title="@string/pref_video_quality" android:summary="@string/pref_summary_video_quality" android:entries="@array/pref_entries_video_quality" android:entryValues="@array/pref_entries_video_quality" /> </PreferenceCategory> </PreferenceScreen> 我也试过DropDownPreference作为标签。 似乎没有任何工作。 当我尝试去应用程序中的我的首选项屏幕时,我总是得到一个Error inflating class DropDownPreference错误。 任何想法如何使用这个DropDownPreference? 谢谢! 编辑:添加错误消息: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.int_a.giantbombforandroid/com.app.int_a.giantbombforandroid.main.SettingsActivity}: java.lang.ClassCastException: android.support.v7.preference.DropDownPreference cannot be cast to android.preference.Preference 编辑:AndroidManifest.xml中的SettingsActivity声明 <activity android:name=".main.SettingsActivity" android:configChanges="orientation|screenSize" android:theme="@style/PreferenceThemeOverlay.v14.Material"> </activity>

哪一个是Kotlin中更好的对象或顶级函数?

我在Tool.kt中加入了一些实用工具,方法A和方法B都可以正常工作。 我想方法B会保存在内存中,当我启动一个应用程序,即使我从来没有调用fun <T> preference(context: Context, name: String, default: T) 我想方法A只调用DelegatesExt.preference(this,"ZipCode",100L)时分配内存 所以我认为方法A比方法B好,对吗? 方法A. object DelegatesExt { fun <T> preference(context: Context, name: String, default: T) = Preference(context, name, default) } class Preference<T>(private val context: Context, private val name: String, private val default: T) { private val prefs: SharedPreferences by lazy { context.getSharedPreferences("default", Context.MODE_PRIVATE) } operator fun getValue(thisRef: […]