如何使用 CPU 在深度睡眠模式下执行任何操作
How to use CPU to perform any operation in Deep Sleep mode
我是 android
的新人。我为我的申请苦苦挣扎了大约 3 周。我需要在正常模式和 sleep mode
下发送和接收数据包。 My app
必须交换数据 5 秒。我尝试使用 alarmmanager
但在 android 5 上它不起作用。在 android
5 上,间隔会在 60 秒内更改它。这样的解决方案使电池很快磨损。当我使用普通异步任务而不是 IntentService
时,它仅在屏幕为 ON
并且 app
可见时才有效。当应用程序被隐藏或我点击电源 OFF
时,交换数据停止工作。什么是最好的解决方案?
您可以使用 AlarmManager
class 在特定时间唤醒设备,然后在您想要的任何时间间隔触发操作。代码 from the docs found here:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
注意这个块的最后一行。您可以使用方法 setRepeating()
来设置您想要的任何时间间隔。
即使是 RTC_WAKEUP 大多数时候也无济于事。
当设备处于深度睡眠模式时适用于我的应用程序的解决方案:
将 WakefulBroadcastReceiver 与 AlarmManager 结合使用。
服务由startWakefulService()启动,完成后调用completeWakefulIntent(intent)释放唤醒锁。因此,设备将被允许重新进入睡眠状态。
我没有添加任何代码。搜索有关如何将 WakefulBroadcastReceiver 与 AlarmManager 结合使用的示例。甚至 WakefulBroadcastReceiver 文档也有一些模板代码。
同时降低警报频率,这样您就可以避免耗尽太多电池电量。
我是 android
的新人。我为我的申请苦苦挣扎了大约 3 周。我需要在正常模式和 sleep mode
下发送和接收数据包。 My app
必须交换数据 5 秒。我尝试使用 alarmmanager
但在 android 5 上它不起作用。在 android
5 上,间隔会在 60 秒内更改它。这样的解决方案使电池很快磨损。当我使用普通异步任务而不是 IntentService
时,它仅在屏幕为 ON
并且 app
可见时才有效。当应用程序被隐藏或我点击电源 OFF
时,交换数据停止工作。什么是最好的解决方案?
您可以使用 AlarmManager
class 在特定时间唤醒设备,然后在您想要的任何时间间隔触发操作。代码 from the docs found here:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
注意这个块的最后一行。您可以使用方法 setRepeating()
来设置您想要的任何时间间隔。
即使是 RTC_WAKEUP 大多数时候也无济于事。
当设备处于深度睡眠模式时适用于我的应用程序的解决方案:
将 WakefulBroadcastReceiver 与 AlarmManager 结合使用。
服务由startWakefulService()启动,完成后调用completeWakefulIntent(intent)释放唤醒锁。因此,设备将被允许重新进入睡眠状态。
我没有添加任何代码。搜索有关如何将 WakefulBroadcastReceiver 与 AlarmManager 结合使用的示例。甚至 WakefulBroadcastReceiver 文档也有一些模板代码。
同时降低警报频率,这样您就可以避免耗尽太多电池电量。