在 Android Java 中调用另一个 class 中的函数

Calling a function in another class in Android Java

我一直在四处寻找这个问题的答案,发现了很多类似的问题,但 none 似乎非常相似,我在网上找到的解决方案中有 none 已解决问题。

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

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

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

进入同步设置class:

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

等 问题是 SharedPreferences 需要扩展上下文,为了让它工作 Android Studio 给出了将 syncSettings 更改为

的错误
public abstract class syncSettings extends Context {

这样做会在调用函数时为 new syncSettings() 带来错误,取消抽象会在 syncSettings class 中带来错误。我该怎么做才能完成这项工作?如果您需要更多信息,请告诉我。

提前致谢。

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

我不明白你扩展上下文十的原因。但是你可以使用这个 class.

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();
        }
    }

试试这个:

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);
          }
    }

在你的activity中:

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