【问题标题】:Calling a function in another class in Android Java在 Android Java 中调用另一个类中的函数
【发布时间】:2020-11-11 12:41:39
【问题描述】:

我一直在寻找这个问题的答案,发现了许多类似的问题,但似乎没有一个足够相似,而且我在网上找到的解决方案都没有解决这个问题。

对于我正在编写的 Android 应用程序,我需要将 Android 首选项保存到数据库中。为了做到这一点,我想创建一个单独的类,其中包含我可以调用以同步、更新等首选项的函数。这就是问题所在

  • 从 mainActivity 调用 syncSettings 类中的initialSaveSettings()

我现在尝试这样做的方法是调用:

 syncSettings sync = new syncSettings();
 sync.initiateSettingSave();

进入syncSettings类:

public class syncSettings extends Context {
      public void initiateSettingsSave(){
            PreferenceManager.setDefaultValues(syncSettings.this, R.xml.root_preferences, false);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
      }
}

等等。 问题是 SharedPreferences 需要 extends Context 并且为了使其正常工作,Android Studio 给出了将 syncSettings 更改为的错误

public abstract class syncSettings extends Context {

这样做会在调用函数时为 new syncSettings() 带来错误,将抽象带走会在 syncSettings 类中产生错误。我能做些什么来完成这项工作?如果您需要更多信息,请告诉我。

提前谢谢你。

编辑:我对 Android 开发非常陌生,所以如果我说或问一些愚蠢的问题,这很可能就是为什么...

【问题讨论】:

  • 您的偏好是用户可以选择的活动?像习惯性的设置活动应用程序通常有吗?
  • 是的,我使用 Android Studio 中的默认设置活动,但我想创建一种在应用打开时将这些设置与数据库同步的方法,因为这些设置似乎会自动保存在本地。跨度>
  • 好的,我明白了,所以您想要将首选项保存到设备外部的远程数据库中?
  • @josap97 我认为您正在从上下文(即活动或服务)调用sync.initiateSettingSave(); 方法。如果我是正确的,那么您应该将方法更改为public void initiateSettingsSave(Context context) 并删除扩展上下文。现在您可以传递活动或服务对象,因为它们都是 Context 的子类。只需从活动/服务中调用sync.initiateSettingSave(this);

标签: java android methods sharedpreferences preferences


【解决方案1】:

试试这个:

public class syncSettings {
         private Context context;

         public syncSettings(Context context){
             this.context = context;
         }
        
          public void initiateSettingsSave(){
                PreferenceManager.setDefaultValues(syncSettings.this, R.xml.root_preferences, false);
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
          }
    }

在你的活动中:

syncSettings sync = new syncSettings(this);
 sync.initiateSettingSave();

【讨论】:

  • 谢谢,这似乎有效。在正确测试之前,我确实需要解决一些其他错误,但至少 Android Studio 不再生气了:)。
  • 是的,这不是您问题的最终解决方案,这只是正确获取 sharedPreferences 的实例,但现在您必须获取这些值并将它们保存在远程数据库中。
【解决方案2】:

我不明白您扩展上下文 10 的原因。但是你可以使用这个类。

import android.content.Context;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    
    public class PrefUtils {
    
        /**
         * Called to save supplied value in shared preferences against given key.
         * @param context Context of caller activity
         * @param key Key of value to save against
         * @param value Value to save
         */
        public static void saveToPrefs(Context context, String key, String value) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            final SharedPreferences.Editor editor = prefs.edit();
            editor.putString(key,value);
            editor.commit();
        }
    
        /**
         * Called to retrieve required value from shared preferences, identified by given key.
         * Default value will be returned of no value found or error occurred.
         * @param context Context of caller activity
         * @param key Key to find value against
         * @param defaultValue Value to return if no data found against given key
         * @return Return the value found against given key, default if not found or any error occurs
         */
        public static String getFromPrefs(Context context, String key, String defaultValue) {
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            try {
                return sharedPrefs.getString(key, defaultValue);
            } catch (Exception e) {
                 e.printStackTrace();
                 return defaultValue;
            }
        }
         /**
         * 
         * @param context Context of caller activity
         * @param key Key to delete from SharedPreferences
         */
        public static void removeFromPrefs(Context context, String key) {
             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
             final SharedPreferences.Editor editor = prefs.edit();
             editor.remove(key);
             editor.commit();
        }
    }

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多