gpt4 book ai didi

android - 修改根据屏幕状态触发业务的告警

转载 作者:行者123 更新时间:2023-11-30 03:33:18 25 4
gpt4 key购买 nike

我目前正在使用广播接收器启动服务,该接收器会在设备启动后 60 秒触发。此广播接收器触发警报,以便我的服务每 60 秒运行一次。代码如下:

public class ScheduleReceiver extends BroadcastReceiver {
// Restart service every 60 seconds
private static final long REPEAT_TIME = 1000 * 60;

@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);

Calendar cal = Calendar.getInstance();
// Start 60 seconds after boot completed
cal.add(Calendar.SECOND, 60);
// Fetch every 60 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
}
}

上面启动服务如下:

public class StartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, PhotoService.class);
context.startService(service);
}
}

我想以某种方式修改它,以便我的服务在手机屏幕打开时每 60 秒运行一次,在关闭时每 20 分钟运行一次。

最好的方法是什么?我可以在屏幕关闭/打开时动态修改警报吗?

感谢您的帮助,

问候,

小狗

最佳答案

好吧,我已经找到了如何做到这一点,所以我想我会把它放在这里以防其他人好奇。 (总是欢迎更优雅的解决方案):

首先,在我的服务中,我将以下内容添加到 OnCreate:

 @Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScheduleReceiver();
registerReceiver(mReceiver, filter);
}

这样做是为了让我的广播接收器可以处理这些事件。显然你不能通过 xml 中的 intent-filters 注册这些。

这意味着当创建服务时,它确保可以处理这些额外的系统操作。

然后我将我的广播接收器修改为:

public class ScheduleReceiver extends BroadcastReceiver {
private static final int ALARM_ID = 909;

@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, ALARM_ID, i,PendingIntent.FLAG_CANCEL_CURRENT);
service.cancel(pending);

Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 60); //Delay startup by one minute - This is useful to prevent over utilization of resources at boot

// Start alarm. Set a long repeat time if the screen is off and a short repeat time if the screen is on
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON) || intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60000, pending);
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 600000, pending);
}
}
}

从上面我取消了所有具有相同未决 Intent 的当前警报,然后每当屏幕关闭/打开时我设置一个新警报。

这似乎对我有用。一如既往地欢迎任何改进。

关于android - 修改根据屏幕状态触发业务的告警,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17049515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com