android闹钟测试工具,如何在Android上测试每日闹钟?

My question is how to test alarms properly against all possible risks? Do instrumentation tests fit here?

由于AlarmManager是Android操作系统的一部分,因此经过充分测试,您无需进行任何仪器测试或单元测试即可对其进行测试.您可以测试由警报调用的服务,但无需测试您的警报设置是否正确.

只有少数情况下您的警报被删除,无效或被忽略.你已经打电话给他们:

>重启时

>按时更改

>打瞌睡模式

现在您可以做的是使用系统通知(OS广播)重新配置警报.

On reboot

设备关闭时会报警,因此请在重启时再次设置

public class BootReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

// set repeating alarm

MyAlarmMangerSupport.set(context);

}

}

}

public class TimeChangeReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

final String action = intent.getAction();

if (Intent.ACTION_TIME_CHANGED.equals(action ) || Intent.ACTION_TIMEZONE_CHANGED.equals(action )) {

// cancel previous alarm and set a new one

MyAlarmMangerSupport.cancelAndSet(context);

}

}

}

On doze mode

我相信如果设备输入the doze mode,则无需显示任何不相关的通知

无论如何,即使设备处于打瞌睡状态,也有一种方法可以发出警报.

重要的是要知道,打盹模式是从API级别23开始的新功能.

>用户必须为您的应用程序whitelist your app and disable Battery Optimization.

>使用setExactAndAllowWhileIdle方法为Android级别2??3的设备设置警报:

alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, period, pendingIntent);

重要提示:作为单次警报,您必须在警报接收器中再次设置它:

public class AlarmReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

final String action = intent.getAction();

if ("my-Pet-Notification".equals(action )) {

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){

// set repeating alarm, calls setExactAndAllowWhileIdle

MyAlarmMangerSupport.set(context);

}

//execute service to show notification

[..]

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值