سؤال

I want to use the method wakeUp of PowerManager. Eclipse (ADT) don't reconize this method. But there is no problem for the opposite "goToSleep" :

PowerManager pm = (PowerManager) MyApplication.getAppContext().getSystemService(Context.POWER_SERVICE);
pm.wakeUp(SystemClock.uptimeMillis()); //Detected as error by eclipse
pm.goToSleep(SystemClock.uptimeMillis()); //Not detected as error and work well

Eclipse error :

The method wakeUp(long) is undefined for the type PowerManager

Eclipse propose to me a quickfix, but i've the same error :

((Object) pm).wakeUp(SystemClock.uptimeMillis()); //the same error 

Is this a bug or just me? Thanks !

هل كانت مفيدة؟

المحلول

First, as Luksprog pointed out, that method is new to API Level 17.

Also, it requires the DEVICE_POWER permission, which can only be held by apps signed by the same signing key as was used to sign the firmware.

نصائح أخرى

I see you have a custom rom. You can use sleep() and wakeUp() from uiautomator, starting with api level 16 to basically achieve the same functionality as with PowerManager wakeUp() and goToSleep() but without being constricted by permissions that you will not be granted by the os (android.permission.DEVICE_POWER).

See this other answer of mine where I explain in more detail what is the setup.

You should have a look at the PowerManager WakeLock API, in particular the flag ACQUIRE_CAUSES_WAKEUP:

ACQUIRE_CAUSES_WAKEUP (Added in API level 1)

public static final int ACQUIRE_CAUSES_WAKEUP

Wake lock flag: Turn the screen on when the wake lock is acquired.

Normally wake locks don't actually wake the device, they just cause the screen to remain on once it's already on. Think of the video player application as the normal behavior. Notifications that pop up and want the device to be on are the exception; use this flag to be like them.

Cannot be used with PARTIAL_WAKE_LOCK.

Constant Value: 268435456 (0x10000000)

Note to use this your App's AndroidMainfest.xml must use the WAKE_LOCK permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />

The API documentation as of API level 29 is rather perplexing because the ACQUIRE_CAUSES_WAKEUP flag must be combined with a valid level, otherwise PowerManager will throw you an IllegalArgumentException : Must specify a valid wake lock level.

Sadly, all available levels have either been deprecated, or are explicitly not allowed to be combined with this flag:

  1. PARTIAL_WAKE_LOCK : Cannot be used with the ACQUIRE_CAUSES_WAKEUP flag
  2. SCREEN_DIM_WAKE_LOCK : Deprecated since API 17
  3. SCREEN_BRIGHT_WAKE_LOCK : Deprecated since API 15
  4. FULL_WAKE_LOCK : Deprecated since API 17
  5. PROXIMITY_SCREEN_OFF_WAKE_LOCK : Cannot be used with the ACQUIRE_CAUSES_WAKEUP flag

So in my app I decided to combine the ACQUIRE_CAUSES_WAKEUP flag with SCREEN_BRIGHT_WAKE_LOCK as being less evil than FULL_WAKE_LOCK and more useful than SCREEN_DIM_WAKE_LOCK.

So my wakeUp implementation looks something like the following (note I have a Preference for users to enable/disable the Wake-Up feature, and also define a method to release the Wake Lock when the app is killed/onDestroy):

import android.content.Context;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.os.PowerManager;

import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import androidx.preference.PreferenceManager;


public class MainActivity extends AppCompatActivity {


    /** Interval of 1 second in milliseconds */
    static final long INTERVAL_SECOND = 1000;

    /** Interval of 1 minute in milliseconds */
    static final long INTERVAL_MINUTE = 60 * INTERVAL_SECOND;

    SharedPreferences sharedPrefs = null;
    PowerManager.WakeLock mWakeLock = null;

    Context applicationContext = null;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String TAG = "onCreate";
        applicationContext = getApplicationContext();

        setContentView(R.layout.activity_main);
        final Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        PreferenceManager
                .setDefaultValues(this, R.xml.root_preferences, false);
        sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    }

    void wakeUp() {
        final String TAG = "wakeUp";
        if (sharedPrefs == null) {
            Log.w(TAG, "Shared Preferences was null");
            return;
        }
        final boolean isWakeUpPref = SettingsActivity.isWakelockPref(sharedPrefs);
        if (!isWakeUpPref) {
            Log.i(TAG, "WakeUp is disabled via preference");
            return;
        }
        Log.i(TAG, "WakeUp is enabled via preference");
        if (mWakeLock == null) {
            Log.i(TAG, "Creating WakeLock...");
            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (pm == null) {
                Log.w(TAG, "could not get Power Service");
                return;
            }
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyApp:wakeup");
            Log.i(TAG, "WakeLock created!");
        } else if (mWakeLock.isHeld()) {
            Log.i(TAG, "Releasing old WakeLock, to reacquire with fresh timeout");
            mWakeLock.release();
        }
        Log.i(TAG, "Acquiring WakeLock to WakeUp...");
        mWakeLock.acquire(INTERVAL_MINUTE);
        Log.i(TAG, "WakeLock WakeUp acquired!");
    }

    void releaseWakeLock() {
        final String TAG = "releaseWakeLock";
        if (mWakeLock == null) {
            return;
        }
        if (!mWakeLock.isHeld()) {
            Log.w(TAG, "WakeLock not held!");
            return;
        }
        mWakeLock.release();
        Log.i(TAG, "WakeLock released!");
    }

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top