android 广播唤醒应用,使设备保持唤醒状态  |  Android 开发者  |  Android Developers...

为避免消耗电池电量,处于空闲状态的 Android 设备会快速进入休眠模式。不过,有时应用需要唤醒屏幕或 CPU 并使之保持唤醒状态,以完成某项工作。

所采用的方法取决于应用的需求。但是,一般而言,您应该为应用使用尽可能轻量的方法,以便最大限度减少应用对系统资源的影响。以下几个部分介绍了如何处理设备的默认休眠行为不符合应用要求的情况。

使用唤醒锁定的替代方案

在为您的应用添加唤醒锁定支持之前,请考虑应用用例是否支持以下某种替代解决方案:

如果您的应用正在执行长时间运行的 HTTP 下载,请考虑使用

如果您的应用同步来自外部服务器的数据,请考虑创建同步适配器。

如果您的应用依赖后台服务,请考虑使用 JobScheduler 或 Firebase 云消息传递,以便以特定的时间间隔触发这些服务。

使屏幕保持开启状态

某些应用需要使屏幕保持开启状态,例如游戏或电影应用。要实现此目标,最好的方法是在您的 Activity 中(仅在 Activity 中,切勿在服务或其他应用组件中)使用

Kotlin

class MainActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

}

}Java

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}

}

这种方法的优点是,与唤醒锁定(在使 CPU 保持运行状态部分介绍)不同,它不需要特殊权限,并且平台可以正确管理用户在不同应用之间的切换,您的应用无需担心释放未使用的资源。

实现此目标的另一种方法是,在应用的布局 XML 文件中,使用

android:layout_width="match_parent"

android:layout_height="match_parent"

android:keepScreenOn="true">

...

使用 android:keepScreenOn="true" 功效等同于

注意:除非您不再希望屏幕在应用运行时保持开启状态(例如,如果您希望屏幕在处于不活动状态一段时间后超时),否则不需要清除 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)。

使 CPU 保持运行状态

如果您需要使 CPU 保持运行状态,以便在设备进入休眠模式之前完成某项工作,可以使用一项称为“唤醒锁定”的

创建和持有唤醒锁定会对主机设备的电池续航时间产生重大影响。因此,您应仅在绝对必要时使用唤醒锁定,并持有尽可能短的时间。例如,您绝不需要在 Activity 中使用唤醒锁定。如上所述,如果您希望屏幕在 Activity 中保持开启状态,请使用

使用唤醒锁定的一种合理情形是,某项后台服务需要获取唤醒锁定,以便 CPU 在屏幕关闭时保持运行状态,可以完成相关工作。再次声明,由于这种做法会影响电池续航时间,因此应尽量减少其使用频率。

如需使用唤醒锁定,首先要将

如果您的应用包含使用服务来完成相关工作的广播接收器,您可以根据使用可使设备保持唤醒状态的广播接收器部分所述,通过

Kotlin

val wakeLock: PowerManager.WakeLock =

(getSystemService(Context.POWER_SERVICE) as PowerManager).run {

newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag").apply {

acquire()

}

}Java

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);

WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,

"MyApp::MyWakelockTag");

wakeLock.acquire();

要释放唤醒锁定,请调用

使用可使设备保持唤醒状态的广播接收器

通过将广播接收器与服务结合使用,您可以管理后台任务的生命周期。

与其他任何广播接收器一样,使用

以下代码使用方法 MyIntentService。该方法与

Kotlin

class MyWakefulReceiver : WakefulBroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

// Start the service, keeping the device awake while the service is

// launching. This is the Intent to deliver to the service.

Intent(context, MyIntentService::class.java).also { service ->

WakefulBroadcastReceiver.startWakefulService(context, service)

}

}

}Java

public class MyWakefulReceiver extends WakefulBroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

// Start the service, keeping the device awake while the service is

// launching. This is the Intent to deliver to the service.

Intent service = new Intent(context, MyIntentService.class);

startWakefulService(context, service);

}

}

Kotlin

const val NOTIFICATION_ID = 1

class MyIntentService : IntentService("MyIntentService") {

private val notificationManager: NotificationManager? = null

internal var builder: NotificationCompat.Builder? = null

override fun onHandleIntent(intent: Intent) {

val extras: Bundle = intent.extras

// Do the work that requires your app to keep the CPU running.

// ...

// Release the wake lock provided by the WakefulBroadcastReceiver.

MyWakefulReceiver.completeWakefulIntent(intent)

}

}Java

public class MyIntentService extends IntentService {

public static final int NOTIFICATION_ID = 1;

private NotificationManager notificationManager;

NotificationCompat.Builder builder;

public MyIntentService() {

super("MyIntentService");

}

@Override

protected void onHandleIntent(Intent intent) {

Bundle extras = intent.getExtras();

// Do the work that requires your app to keep the CPU running.

// ...

// Release the wake lock provided by the WakefulBroadcastReceiver.

MyWakefulReceiver.completeWakefulIntent(intent);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值