如何在Android中编写应用程序以始终保持屏幕活动? (始终处于交互模式)

时间:2016-09-27 06:27:08

标签: wear-os interactive-mode

我需要知道是否有办法让Android磨损应用程序始终处于交互模式。 (我知道这会耗尽电池寿命)

'旋转离开'手表是否可以按压,或者只要手表在外侧进入,手表是否会进入环境?

1 个答案:

答案 0 :(得分:1)

答案取决于您的" app"是一项活动或服务。

活动应该只需指定android:keepScreenOn="true",而无需进一步操作。

如果它是一项服务(例如,您正在制作闹钟/计时器表盘),我建议您启动一个透明的"永远在线的"活动。 AFAIK,目前,系统不允许您的服务通过超出正常屏幕开启周期持续时间的完全唤醒锁定来保持屏幕开启。这可能是由于唤醒锁误用造成的。使用活动,这当前是可能的,并且具有为您处理解雇操作的好处(例如,侧按钮按钮,手掌等)。我在一组"计时器"中使用了类似的模式。看脸。

以下是如何通过服务实现永远在线的方式:

在负责唤醒手表的服务/活动中:

private PowerManager.WakeLock mFullWakeLock;

// Initialization...
PowerManager pm =(PowerManager) getSystemService(Context.POWER_SERVICE);
mFullWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "WatchFaceAlarmWokenUp");

// Elsewhere, when the watch needs to be woken by the service...
// Wake the watch to give ourselves some time to start the activity
mFullWakeLock.acquire(2000l);

Intent intent = new Intent(this, AlarmExpiredActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

您的AlarmExpiredActivity布局必须指定以下属性: android:keepScreenOn="true"

我建议您同时指定android:onClick="hide",以便用户更容易杀死耗尽观看费用的内容。