Search code examples
javaandroidandroid-contentresolver

How to use Content Resolver passed into a static method as an argument


Very sorry if I've missed a solution that is along these lines. I am a question asking noob here, but have visited many times for research, and I did search, I promise.

I'm trying to add a method to use upon a reboot to restore a kernel node which controls the activation/deactivation of hardware capacitive keys on an affected Android device. I've created a method to do this inside of my HardwareKeysSettings.java class:

public static void restore(Context context) {
        boolean enableHardwareKeys = Settings.System.getInt(getContentResolver(),
                Settings.System.HARDWARE_KEYS_ENABLED, 1) == 1;
        Settings.System.putInt(getContentResolver(),
            Settings.System.HARDWARE_KEYS_ENABLED, enableHardwareKeys ? 1 : 0);
}

And my method is called from a BootReceiver class:

    package com.android.settings.slim;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.android.settings.slim.HardwareKeysSettings;

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent intent) {
        /* Restore the hardware tunable values */
        HardwareKeysSettings.restore(ctx);
    }
}

I can't compile this, because in my restore() method above, the getContentResolver() method can't be used within a static method (which I need to use). I get these errors:

/packages/apps/Settings/src/com/android/settings/slim/HardwareKeysSettings.java:676: Cannot make a static reference to the non-static method getContentResolver() from the type SettingsPreferenceFragment

/packages/apps/Settings/src/com/android/settings/slim/HardwareKeysSettings.java:678: Cannot make a static reference to the non-static method getContentResolver() from the type SettingsPreferenceFragment

Not surprising or terribly abnormal issues there. Speaking with someone who is way more knowledgeable about this than me, I was only given this hint... to

"call your content resolver from the context passed as a arg"

which makes sense to me since obviously the getContentResolver() method is non-static and can't be used inside my static method. I need to pass in something in order to use the getContentResolver() method properly.

So, the question is, how exactly do I do this? I've got somewhat of an idea, but ContentResolver is among the most confusing of Android/java things to me.

I'm kind of thinking this means passing in ContentResolver like this, but no clue how to use it internally for my purpose:

public static void restore(Context context, ContentResolver contentResolver) {

Thanks in advance... :)


Solution

  • how exactly do I do this?

    getContentResolver() is a method on Context. You are passing in a Context to restore(). Call getContentResolver() on that Context:

    public static void restore(Context context) {
        boolean enableHardwareKeys = Settings.System.getInt(context.getContentResolver(),
                Settings.System.HARDWARE_KEYS_ENABLED, 1) == 1;
        Settings.System.putInt(context.getContentResolver(),
            Settings.System.HARDWARE_KEYS_ENABLED, enableHardwareKeys ? 1 : 0);
    }