您应该查看 PowerManager WakeLock API,特别是标志ACQUIRE_CAUSES_WAKEUP:
ACQUIRE_CAUSES_WAKEUP(在 API 级别 1 中添加)
public static final int ACQUIRE_CAUSES_WAKEUP
唤醒锁定标志:获得唤醒锁定时打开屏幕。
通常,唤醒锁实际上不会唤醒设备,它们只会使屏幕在它已经打开时保持打开状态。将视频播放器应用程序视为正常行为。弹出并希望设备开启的通知是例外;使用这面旗帜成为他们的样子。
不能与 PARTIAL_WAKE_LOCK 一起使用。
常数值:268435456 (0x10000000)
注意使用这个你的应用程序AndroidMainfest.xml
必须使用WAKE_LOCK
权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
API 级别 29 的 API 文档相当令人困惑,因为ACQUIRE_CAUSES_WAKEUP
标志必须与有效级别组合,否则PowerManager
会抛出IllegalArgumentException
:必须指定有效的唤醒锁定级别。
遗憾的是,所有可用级别要么已被弃用,要么明确不允许与此标志结合使用:
PARTIAL_WAKE_LOCK
: 不能与ACQUIRE_CAUSES_WAKEUP
标志一起使用
SCREEN_DIM_WAKE_LOCK
: 自 API 17 起已弃用
SCREEN_BRIGHT_WAKE_LOCK
: 自 API 15 起已弃用
FULL_WAKE_LOCK
: 自 API 17 起已弃用
PROXIMITY_SCREEN_OFF_WAKE_LOCK
: 不能与ACQUIRE_CAUSES_WAKEUP
标志一起使用
因此,在我的应用程序中,我决定将ACQUIRE_CAUSES_WAKEUP
标志与SCREEN_BRIGHT_WAKE_LOCK
asFULL_WAKE_LOCK
比SCREEN_DIM_WAKE_LOCK
.
所以我的 wakeUp 实现如下所示(请注意,我有一个让用户启用/禁用唤醒功能的首选项,并且还定义了一个在应用程序被杀死/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!");
}
}